History log of /netbsd-current/sys/dev/audio/audio.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.146 27-May-2024 nia

audio(4): Handle delta == 0 in audio_volume_(up|down).

These functions are used by the pmf power handler functions to deal
with keyboard volume up/down key presses.

However, delta is optional, and may not be returned by certain hardware
drivers, including the earliest reference drivers (it is unlikely that
these are used on any systems with pmf volume keys on the keyboard,
but still).

XXX it may make sense to have 16 as a minimum bound for delta, since
pressing the volume up key on a keyboard 255 times doesn't sound
particularly fun.


Revision tags: thorpej-ifq-base thorpej-altq-separation-base
# 1.145 01-Oct-2023 mlelstv

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


# 1.144 05-Jun-2023 mlelstv

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


# 1.143 23-Apr-2023 mlelstv

print stage formats with AUDIO_DEBUG


# 1.142 23-Apr-2023 mlelstv

Be a little bit more informative on device timeout.


# 1.141 23-Apr-2023 mlelstv

Whitespace


# 1.140 23-Apr-2023 mlelstv

Add 24bit/32bit hardware support.


# 1.139 23-Apr-2023 mlelstv

Whitespace


# 1.138 23-Apr-2023 mlelstv

Make audio_device information available to drvctl and devpubd.


# 1.137 17-Apr-2023 mlelstv

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


Revision tags: netbsd-10-0-RC6 netbsd-10-0-RC5 netbsd-10-0-RC4 netbsd-10-0-RC3 netbsd-10-0-RC2 netbsd-10-0-RC1 netbsd-10-base bouyer-sunxi-drm-base
# 1.136 25-Aug-2022 riastradh

branches: 1.136.4;
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+6634ffd48997ae9b1eb0@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=db40a795a0b078f9b3b9fa0d3b7a9addcd2534de

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

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

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

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

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


# 1.135 13-Aug-2022 isaki

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.


# 1.134 06-Jul-2022 riastradh

uvm(9): fo_mmap caller guarantees positive size.

No functional change intended, just sprinkling assertions to make it
clearer.


# 1.133 23-Apr-2022 isaki

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


# 1.132 23-Apr-2022 isaki

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.


# 1.131 23-Apr-2022 isaki

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


# 1.130 23-Apr-2022 isaki

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


# 1.129 23-Apr-2022 isaki

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.


# 1.128 21-Apr-2022 macallan

#if 0 now unused audio_track_is_record() to appease clang


# 1.127 20-Apr-2022 isaki

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.


# 1.126 20-Apr-2022 isaki

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).


# 1.125 20-Apr-2022 isaki

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


# 1.124 19-Apr-2022 riastradh

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+31d2619e72c2c8436cc9@syzkaller.appspotmail.com


# 1.123 09-Apr-2022 riastradh

audio(4): Use membar_acquire, not membar_enter.

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


# 1.122 31-Mar-2022 pgoyette

For device modules that provide both auto-config and /dev/xxx
interfaces, make sure that initialization and destruction
follow the proper sequence. This is triggered by the recent
changes to the devsw stuff; per riastradh@ the required call
sequence is:

devsw_attach()
config_init_component() or config_cf*_attach()
...
config_fini_component() or config_cf*_detach()
devsw_detach()

While here, add a few missing calls to some of the detach
routines.

Testing of these changes has been limited to:
1. compile without build break
2. no related test failures from atf
3. modload/modunload work as well as
before.

No functional device testing done, since I don't have any
of these devices. Let me know of any damage I might cause
here!

XXX Some of the modules affected by this commit are already
XXX broken; see kern/56772. This commit does not break
any additional modules (as far as I know).


# 1.121 28-Mar-2022 riastradh

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


# 1.120 26-Mar-2022 isaki

Add terminology comments.


# 1.119 26-Mar-2022 isaki

Improve comments.


# 1.118 26-Mar-2022 isaki

Remove a dead code in audio_track_record().


# 1.117 26-Mar-2022 isaki

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.


# 1.116 26-Mar-2022 isaki

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.


# 1.115 14-Mar-2022 riastradh

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

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


# 1.114 14-Mar-2022 riastradh

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.


# 1.113 12-Dec-2021 andvar

fix various typos, mainly in comments.


# 1.112 10-Dec-2021 andvar

s/occured/occurred/ in comments, log messages and man pages.


# 1.111 05-Dec-2021 msaitoh

s/faciliate/facilitate/ in comment.


# 1.110 10-Oct-2021 riastradh

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!


# 1.109 10-Oct-2021 riastradh

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.


# 1.108 26-Sep-2021 thorpej

Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.145 01-Oct-2023 mlelstv

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


# 1.144 05-Jun-2023 mlelstv

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


# 1.143 23-Apr-2023 mlelstv

print stage formats with AUDIO_DEBUG


# 1.142 23-Apr-2023 mlelstv

Be a little bit more informative on device timeout.


# 1.141 23-Apr-2023 mlelstv

Whitespace


# 1.140 23-Apr-2023 mlelstv

Add 24bit/32bit hardware support.


# 1.139 23-Apr-2023 mlelstv

Whitespace


# 1.138 23-Apr-2023 mlelstv

Make audio_device information available to drvctl and devpubd.


# 1.137 17-Apr-2023 mlelstv

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


Revision tags: netbsd-10-base bouyer-sunxi-drm-base
# 1.136 25-Aug-2022 riastradh

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+6634ffd48997ae9b1eb0@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=db40a795a0b078f9b3b9fa0d3b7a9addcd2534de

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

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

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

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

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


# 1.135 13-Aug-2022 isaki

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.


# 1.134 06-Jul-2022 riastradh

uvm(9): fo_mmap caller guarantees positive size.

No functional change intended, just sprinkling assertions to make it
clearer.


# 1.133 23-Apr-2022 isaki

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


# 1.132 23-Apr-2022 isaki

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.


# 1.131 23-Apr-2022 isaki

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


# 1.130 23-Apr-2022 isaki

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


# 1.129 23-Apr-2022 isaki

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.


# 1.128 21-Apr-2022 macallan

#if 0 now unused audio_track_is_record() to appease clang


# 1.127 20-Apr-2022 isaki

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.


# 1.126 20-Apr-2022 isaki

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).


# 1.125 20-Apr-2022 isaki

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


# 1.124 19-Apr-2022 riastradh

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+31d2619e72c2c8436cc9@syzkaller.appspotmail.com


# 1.123 09-Apr-2022 riastradh

audio(4): Use membar_acquire, not membar_enter.

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


# 1.122 31-Mar-2022 pgoyette

For device modules that provide both auto-config and /dev/xxx
interfaces, make sure that initialization and destruction
follow the proper sequence. This is triggered by the recent
changes to the devsw stuff; per riastradh@ the required call
sequence is:

devsw_attach()
config_init_component() or config_cf*_attach()
...
config_fini_component() or config_cf*_detach()
devsw_detach()

While here, add a few missing calls to some of the detach
routines.

Testing of these changes has been limited to:
1. compile without build break
2. no related test failures from atf
3. modload/modunload work as well as
before.

No functional device testing done, since I don't have any
of these devices. Let me know of any damage I might cause
here!

XXX Some of the modules affected by this commit are already
XXX broken; see kern/56772. This commit does not break
any additional modules (as far as I know).


# 1.121 28-Mar-2022 riastradh

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


# 1.120 26-Mar-2022 isaki

Add terminology comments.


# 1.119 26-Mar-2022 isaki

Improve comments.


# 1.118 26-Mar-2022 isaki

Remove a dead code in audio_track_record().


# 1.117 26-Mar-2022 isaki

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.


# 1.116 26-Mar-2022 isaki

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.


# 1.115 14-Mar-2022 riastradh

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

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


# 1.114 14-Mar-2022 riastradh

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.


# 1.113 12-Dec-2021 andvar

fix various typos, mainly in comments.


# 1.112 10-Dec-2021 andvar

s/occured/occurred/ in comments, log messages and man pages.


# 1.111 05-Dec-2021 msaitoh

s/faciliate/facilitate/ in comment.


# 1.110 10-Oct-2021 riastradh

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!


# 1.109 10-Oct-2021 riastradh

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.


# 1.108 26-Sep-2021 thorpej

Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.144 05-Jun-2023 mlelstv

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


# 1.143 23-Apr-2023 mlelstv

print stage formats with AUDIO_DEBUG


# 1.142 23-Apr-2023 mlelstv

Be a little bit more informative on device timeout.


# 1.141 23-Apr-2023 mlelstv

Whitespace


# 1.140 23-Apr-2023 mlelstv

Add 24bit/32bit hardware support.


# 1.139 23-Apr-2023 mlelstv

Whitespace


# 1.138 23-Apr-2023 mlelstv

Make audio_device information available to drvctl and devpubd.


# 1.137 17-Apr-2023 mlelstv

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


Revision tags: netbsd-10-base bouyer-sunxi-drm-base
# 1.136 25-Aug-2022 riastradh

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+6634ffd48997ae9b1eb0@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=db40a795a0b078f9b3b9fa0d3b7a9addcd2534de

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

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

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

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

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


# 1.135 13-Aug-2022 isaki

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.


# 1.134 06-Jul-2022 riastradh

uvm(9): fo_mmap caller guarantees positive size.

No functional change intended, just sprinkling assertions to make it
clearer.


# 1.133 23-Apr-2022 isaki

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


# 1.132 23-Apr-2022 isaki

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.


# 1.131 23-Apr-2022 isaki

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


# 1.130 23-Apr-2022 isaki

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


# 1.129 23-Apr-2022 isaki

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.


# 1.128 21-Apr-2022 macallan

#if 0 now unused audio_track_is_record() to appease clang


# 1.127 20-Apr-2022 isaki

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.


# 1.126 20-Apr-2022 isaki

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).


# 1.125 20-Apr-2022 isaki

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


# 1.124 19-Apr-2022 riastradh

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+31d2619e72c2c8436cc9@syzkaller.appspotmail.com


# 1.123 09-Apr-2022 riastradh

audio(4): Use membar_acquire, not membar_enter.

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


# 1.122 31-Mar-2022 pgoyette

For device modules that provide both auto-config and /dev/xxx
interfaces, make sure that initialization and destruction
follow the proper sequence. This is triggered by the recent
changes to the devsw stuff; per riastradh@ the required call
sequence is:

devsw_attach()
config_init_component() or config_cf*_attach()
...
config_fini_component() or config_cf*_detach()
devsw_detach()

While here, add a few missing calls to some of the detach
routines.

Testing of these changes has been limited to:
1. compile without build break
2. no related test failures from atf
3. modload/modunload work as well as
before.

No functional device testing done, since I don't have any
of these devices. Let me know of any damage I might cause
here!

XXX Some of the modules affected by this commit are already
XXX broken; see kern/56772. This commit does not break
any additional modules (as far as I know).


# 1.121 28-Mar-2022 riastradh

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


# 1.120 26-Mar-2022 isaki

Add terminology comments.


# 1.119 26-Mar-2022 isaki

Improve comments.


# 1.118 26-Mar-2022 isaki

Remove a dead code in audio_track_record().


# 1.117 26-Mar-2022 isaki

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.


# 1.116 26-Mar-2022 isaki

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.


# 1.115 14-Mar-2022 riastradh

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

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


# 1.114 14-Mar-2022 riastradh

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.


# 1.113 12-Dec-2021 andvar

fix various typos, mainly in comments.


# 1.112 10-Dec-2021 andvar

s/occured/occurred/ in comments, log messages and man pages.


# 1.111 05-Dec-2021 msaitoh

s/faciliate/facilitate/ in comment.


# 1.110 10-Oct-2021 riastradh

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!


# 1.109 10-Oct-2021 riastradh

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.


# 1.108 26-Sep-2021 thorpej

Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.143 23-Apr-2023 mlelstv

print stage formats with AUDIO_DEBUG


# 1.142 23-Apr-2023 mlelstv

Be a little bit more informative on device timeout.


# 1.141 23-Apr-2023 mlelstv

Whitespace


# 1.140 23-Apr-2023 mlelstv

Add 24bit/32bit hardware support.


# 1.139 23-Apr-2023 mlelstv

Whitespace


# 1.138 23-Apr-2023 mlelstv

Make audio_device information available to drvctl and devpubd.


# 1.137 17-Apr-2023 mlelstv

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


Revision tags: netbsd-10-base bouyer-sunxi-drm-base
# 1.136 25-Aug-2022 riastradh

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+6634ffd48997ae9b1eb0@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=db40a795a0b078f9b3b9fa0d3b7a9addcd2534de

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

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

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

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

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


# 1.135 13-Aug-2022 isaki

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.


# 1.134 06-Jul-2022 riastradh

uvm(9): fo_mmap caller guarantees positive size.

No functional change intended, just sprinkling assertions to make it
clearer.


# 1.133 23-Apr-2022 isaki

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


# 1.132 23-Apr-2022 isaki

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.


# 1.131 23-Apr-2022 isaki

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


# 1.130 23-Apr-2022 isaki

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


# 1.129 23-Apr-2022 isaki

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.


# 1.128 21-Apr-2022 macallan

#if 0 now unused audio_track_is_record() to appease clang


# 1.127 20-Apr-2022 isaki

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.


# 1.126 20-Apr-2022 isaki

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).


# 1.125 20-Apr-2022 isaki

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


# 1.124 19-Apr-2022 riastradh

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+31d2619e72c2c8436cc9@syzkaller.appspotmail.com


# 1.123 09-Apr-2022 riastradh

audio(4): Use membar_acquire, not membar_enter.

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


# 1.122 31-Mar-2022 pgoyette

For device modules that provide both auto-config and /dev/xxx
interfaces, make sure that initialization and destruction
follow the proper sequence. This is triggered by the recent
changes to the devsw stuff; per riastradh@ the required call
sequence is:

devsw_attach()
config_init_component() or config_cf*_attach()
...
config_fini_component() or config_cf*_detach()
devsw_detach()

While here, add a few missing calls to some of the detach
routines.

Testing of these changes has been limited to:
1. compile without build break
2. no related test failures from atf
3. modload/modunload work as well as
before.

No functional device testing done, since I don't have any
of these devices. Let me know of any damage I might cause
here!

XXX Some of the modules affected by this commit are already
XXX broken; see kern/56772. This commit does not break
any additional modules (as far as I know).


# 1.121 28-Mar-2022 riastradh

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


# 1.120 26-Mar-2022 isaki

Add terminology comments.


# 1.119 26-Mar-2022 isaki

Improve comments.


# 1.118 26-Mar-2022 isaki

Remove a dead code in audio_track_record().


# 1.117 26-Mar-2022 isaki

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.


# 1.116 26-Mar-2022 isaki

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.


# 1.115 14-Mar-2022 riastradh

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

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


# 1.114 14-Mar-2022 riastradh

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.


# 1.113 12-Dec-2021 andvar

fix various typos, mainly in comments.


# 1.112 10-Dec-2021 andvar

s/occured/occurred/ in comments, log messages and man pages.


# 1.111 05-Dec-2021 msaitoh

s/faciliate/facilitate/ in comment.


# 1.110 10-Oct-2021 riastradh

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!


# 1.109 10-Oct-2021 riastradh

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.


# 1.108 26-Sep-2021 thorpej

Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.137 17-Apr-2023 mlelstv

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


Revision tags: netbsd-10-base bouyer-sunxi-drm-base
# 1.136 25-Aug-2022 riastradh

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+6634ffd48997ae9b1eb0@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=db40a795a0b078f9b3b9fa0d3b7a9addcd2534de

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

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

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

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

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


# 1.135 13-Aug-2022 isaki

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.


# 1.134 06-Jul-2022 riastradh

uvm(9): fo_mmap caller guarantees positive size.

No functional change intended, just sprinkling assertions to make it
clearer.


# 1.133 23-Apr-2022 isaki

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


# 1.132 23-Apr-2022 isaki

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.


# 1.131 23-Apr-2022 isaki

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


# 1.130 23-Apr-2022 isaki

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


# 1.129 23-Apr-2022 isaki

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.


# 1.128 21-Apr-2022 macallan

#if 0 now unused audio_track_is_record() to appease clang


# 1.127 20-Apr-2022 isaki

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.


# 1.126 20-Apr-2022 isaki

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).


# 1.125 20-Apr-2022 isaki

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


# 1.124 19-Apr-2022 riastradh

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+31d2619e72c2c8436cc9@syzkaller.appspotmail.com


# 1.123 09-Apr-2022 riastradh

audio(4): Use membar_acquire, not membar_enter.

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


# 1.122 31-Mar-2022 pgoyette

For device modules that provide both auto-config and /dev/xxx
interfaces, make sure that initialization and destruction
follow the proper sequence. This is triggered by the recent
changes to the devsw stuff; per riastradh@ the required call
sequence is:

devsw_attach()
config_init_component() or config_cf*_attach()
...
config_fini_component() or config_cf*_detach()
devsw_detach()

While here, add a few missing calls to some of the detach
routines.

Testing of these changes has been limited to:
1. compile without build break
2. no related test failures from atf
3. modload/modunload work as well as
before.

No functional device testing done, since I don't have any
of these devices. Let me know of any damage I might cause
here!

XXX Some of the modules affected by this commit are already
XXX broken; see kern/56772. This commit does not break
any additional modules (as far as I know).


# 1.121 28-Mar-2022 riastradh

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


# 1.120 26-Mar-2022 isaki

Add terminology comments.


# 1.119 26-Mar-2022 isaki

Improve comments.


# 1.118 26-Mar-2022 isaki

Remove a dead code in audio_track_record().


# 1.117 26-Mar-2022 isaki

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.


# 1.116 26-Mar-2022 isaki

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.


# 1.115 14-Mar-2022 riastradh

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

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


# 1.114 14-Mar-2022 riastradh

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.


# 1.113 12-Dec-2021 andvar

fix various typos, mainly in comments.


# 1.112 10-Dec-2021 andvar

s/occured/occurred/ in comments, log messages and man pages.


# 1.111 05-Dec-2021 msaitoh

s/faciliate/facilitate/ in comment.


# 1.110 10-Oct-2021 riastradh

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!


# 1.109 10-Oct-2021 riastradh

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.


# 1.108 26-Sep-2021 thorpej

Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.136 25-Aug-2022 riastradh

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+6634ffd48997ae9b1eb0@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=db40a795a0b078f9b3b9fa0d3b7a9addcd2534de

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

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

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

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

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


# 1.135 13-Aug-2022 isaki

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.


# 1.134 06-Jul-2022 riastradh

uvm(9): fo_mmap caller guarantees positive size.

No functional change intended, just sprinkling assertions to make it
clearer.


# 1.133 23-Apr-2022 isaki

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


# 1.132 23-Apr-2022 isaki

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.


# 1.131 23-Apr-2022 isaki

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


# 1.130 23-Apr-2022 isaki

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


# 1.129 23-Apr-2022 isaki

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.


# 1.128 21-Apr-2022 macallan

#if 0 now unused audio_track_is_record() to appease clang


# 1.127 20-Apr-2022 isaki

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.


# 1.126 20-Apr-2022 isaki

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).


# 1.125 20-Apr-2022 isaki

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


# 1.124 19-Apr-2022 riastradh

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+31d2619e72c2c8436cc9@syzkaller.appspotmail.com


# 1.123 09-Apr-2022 riastradh

audio(4): Use membar_acquire, not membar_enter.

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


# 1.122 31-Mar-2022 pgoyette

For device modules that provide both auto-config and /dev/xxx
interfaces, make sure that initialization and destruction
follow the proper sequence. This is triggered by the recent
changes to the devsw stuff; per riastradh@ the required call
sequence is:

devsw_attach()
config_init_component() or config_cf*_attach()
...
config_fini_component() or config_cf*_detach()
devsw_detach()

While here, add a few missing calls to some of the detach
routines.

Testing of these changes has been limited to:
1. compile without build break
2. no related test failures from atf
3. modload/modunload work as well as
before.

No functional device testing done, since I don't have any
of these devices. Let me know of any damage I might cause
here!

XXX Some of the modules affected by this commit are already
XXX broken; see kern/56772. This commit does not break
any additional modules (as far as I know).


# 1.121 28-Mar-2022 riastradh

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


# 1.120 26-Mar-2022 isaki

Add terminology comments.


# 1.119 26-Mar-2022 isaki

Improve comments.


# 1.118 26-Mar-2022 isaki

Remove a dead code in audio_track_record().


# 1.117 26-Mar-2022 isaki

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.


# 1.116 26-Mar-2022 isaki

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.


# 1.115 14-Mar-2022 riastradh

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

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


# 1.114 14-Mar-2022 riastradh

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.


# 1.113 12-Dec-2021 andvar

fix various typos, mainly in comments.


# 1.112 10-Dec-2021 andvar

s/occured/occurred/ in comments, log messages and man pages.


# 1.111 05-Dec-2021 msaitoh

s/faciliate/facilitate/ in comment.


# 1.110 10-Oct-2021 riastradh

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!


# 1.109 10-Oct-2021 riastradh

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.


# 1.108 26-Sep-2021 thorpej

Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.135 13-Aug-2022 isaki

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.


# 1.134 06-Jul-2022 riastradh

uvm(9): fo_mmap caller guarantees positive size.

No functional change intended, just sprinkling assertions to make it
clearer.


# 1.133 23-Apr-2022 isaki

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


# 1.132 23-Apr-2022 isaki

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.


# 1.131 23-Apr-2022 isaki

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


# 1.130 23-Apr-2022 isaki

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


# 1.129 23-Apr-2022 isaki

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.


# 1.128 21-Apr-2022 macallan

#if 0 now unused audio_track_is_record() to appease clang


# 1.127 20-Apr-2022 isaki

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.


# 1.126 20-Apr-2022 isaki

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).


# 1.125 20-Apr-2022 isaki

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


# 1.124 19-Apr-2022 riastradh

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+31d2619e72c2c8436cc9@syzkaller.appspotmail.com


# 1.123 09-Apr-2022 riastradh

audio(4): Use membar_acquire, not membar_enter.

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


# 1.122 31-Mar-2022 pgoyette

For device modules that provide both auto-config and /dev/xxx
interfaces, make sure that initialization and destruction
follow the proper sequence. This is triggered by the recent
changes to the devsw stuff; per riastradh@ the required call
sequence is:

devsw_attach()
config_init_component() or config_cf*_attach()
...
config_fini_component() or config_cf*_detach()
devsw_detach()

While here, add a few missing calls to some of the detach
routines.

Testing of these changes has been limited to:
1. compile without build break
2. no related test failures from atf
3. modload/modunload work as well as
before.

No functional device testing done, since I don't have any
of these devices. Let me know of any damage I might cause
here!

XXX Some of the modules affected by this commit are already
XXX broken; see kern/56772. This commit does not break
any additional modules (as far as I know).


# 1.121 28-Mar-2022 riastradh

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


# 1.120 26-Mar-2022 isaki

Add terminology comments.


# 1.119 26-Mar-2022 isaki

Improve comments.


# 1.118 26-Mar-2022 isaki

Remove a dead code in audio_track_record().


# 1.117 26-Mar-2022 isaki

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.


# 1.116 26-Mar-2022 isaki

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.


# 1.115 14-Mar-2022 riastradh

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

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


# 1.114 14-Mar-2022 riastradh

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.


# 1.113 12-Dec-2021 andvar

fix various typos, mainly in comments.


# 1.112 10-Dec-2021 andvar

s/occured/occurred/ in comments, log messages and man pages.


# 1.111 05-Dec-2021 msaitoh

s/faciliate/facilitate/ in comment.


# 1.110 10-Oct-2021 riastradh

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!


# 1.109 10-Oct-2021 riastradh

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.


# 1.108 26-Sep-2021 thorpej

Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.134 06-Jul-2022 riastradh

uvm(9): fo_mmap caller guarantees positive size.

No functional change intended, just sprinkling assertions to make it
clearer.


# 1.133 23-Apr-2022 isaki

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


# 1.132 23-Apr-2022 isaki

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.


# 1.131 23-Apr-2022 isaki

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


# 1.130 23-Apr-2022 isaki

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


# 1.129 23-Apr-2022 isaki

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.


# 1.128 21-Apr-2022 macallan

#if 0 now unused audio_track_is_record() to appease clang


# 1.127 20-Apr-2022 isaki

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.


# 1.126 20-Apr-2022 isaki

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).


# 1.125 20-Apr-2022 isaki

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


# 1.124 19-Apr-2022 riastradh

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+31d2619e72c2c8436cc9@syzkaller.appspotmail.com


# 1.123 09-Apr-2022 riastradh

audio(4): Use membar_acquire, not membar_enter.

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


# 1.122 31-Mar-2022 pgoyette

For device modules that provide both auto-config and /dev/xxx
interfaces, make sure that initialization and destruction
follow the proper sequence. This is triggered by the recent
changes to the devsw stuff; per riastradh@ the required call
sequence is:

devsw_attach()
config_init_component() or config_cf*_attach()
...
config_fini_component() or config_cf*_detach()
devsw_detach()

While here, add a few missing calls to some of the detach
routines.

Testing of these changes has been limited to:
1. compile without build break
2. no related test failures from atf
3. modload/modunload work as well as
before.

No functional device testing done, since I don't have any
of these devices. Let me know of any damage I might cause
here!

XXX Some of the modules affected by this commit are already
XXX broken; see kern/56772. This commit does not break
any additional modules (as far as I know).


# 1.121 28-Mar-2022 riastradh

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


# 1.120 26-Mar-2022 isaki

Add terminology comments.


# 1.119 26-Mar-2022 isaki

Improve comments.


# 1.118 26-Mar-2022 isaki

Remove a dead code in audio_track_record().


# 1.117 26-Mar-2022 isaki

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.


# 1.116 26-Mar-2022 isaki

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.


# 1.115 14-Mar-2022 riastradh

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

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


# 1.114 14-Mar-2022 riastradh

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.


# 1.113 12-Dec-2021 andvar

fix various typos, mainly in comments.


# 1.112 10-Dec-2021 andvar

s/occured/occurred/ in comments, log messages and man pages.


# 1.111 05-Dec-2021 msaitoh

s/faciliate/facilitate/ in comment.


# 1.110 10-Oct-2021 riastradh

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!


# 1.109 10-Oct-2021 riastradh

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.


# 1.108 26-Sep-2021 thorpej

Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.133 23-Apr-2022 isaki

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


# 1.132 23-Apr-2022 isaki

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.


# 1.131 23-Apr-2022 isaki

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


# 1.130 23-Apr-2022 isaki

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


# 1.129 23-Apr-2022 isaki

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.


# 1.128 21-Apr-2022 macallan

#if 0 now unused audio_track_is_record() to appease clang


# 1.127 20-Apr-2022 isaki

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.


# 1.126 20-Apr-2022 isaki

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).


# 1.125 20-Apr-2022 isaki

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


# 1.124 19-Apr-2022 riastradh

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+31d2619e72c2c8436cc9@syzkaller.appspotmail.com


# 1.123 09-Apr-2022 riastradh

audio(4): Use membar_acquire, not membar_enter.

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


# 1.122 31-Mar-2022 pgoyette

For device modules that provide both auto-config and /dev/xxx
interfaces, make sure that initialization and destruction
follow the proper sequence. This is triggered by the recent
changes to the devsw stuff; per riastradh@ the required call
sequence is:

devsw_attach()
config_init_component() or config_cf*_attach()
...
config_fini_component() or config_cf*_detach()
devsw_detach()

While here, add a few missing calls to some of the detach
routines.

Testing of these changes has been limited to:
1. compile without build break
2. no related test failures from atf
3. modload/modunload work as well as
before.

No functional device testing done, since I don't have any
of these devices. Let me know of any damage I might cause
here!

XXX Some of the modules affected by this commit are already
XXX broken; see kern/56772. This commit does not break
any additional modules (as far as I know).


# 1.121 28-Mar-2022 riastradh

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


# 1.120 26-Mar-2022 isaki

Add terminology comments.


# 1.119 26-Mar-2022 isaki

Improve comments.


# 1.118 26-Mar-2022 isaki

Remove a dead code in audio_track_record().


# 1.117 26-Mar-2022 isaki

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.


# 1.116 26-Mar-2022 isaki

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.


# 1.115 14-Mar-2022 riastradh

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

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


# 1.114 14-Mar-2022 riastradh

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.


# 1.113 12-Dec-2021 andvar

fix various typos, mainly in comments.


# 1.112 10-Dec-2021 andvar

s/occured/occurred/ in comments, log messages and man pages.


# 1.111 05-Dec-2021 msaitoh

s/faciliate/facilitate/ in comment.


# 1.110 10-Oct-2021 riastradh

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!


# 1.109 10-Oct-2021 riastradh

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.


# 1.108 26-Sep-2021 thorpej

Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.133 23-Apr-2022 isaki

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


# 1.132 23-Apr-2022 isaki

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.


# 1.131 23-Apr-2022 isaki

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


# 1.130 23-Apr-2022 isaki

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


# 1.129 23-Apr-2022 isaki

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.


# 1.128 21-Apr-2022 macallan

#if 0 now unused audio_track_is_record() to appease clang


# 1.127 20-Apr-2022 isaki

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.


# 1.126 20-Apr-2022 isaki

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).


# 1.125 20-Apr-2022 isaki

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


# 1.124 19-Apr-2022 riastradh

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+31d2619e72c2c8436cc9@syzkaller.appspotmail.com


# 1.123 09-Apr-2022 riastradh

audio(4): Use membar_acquire, not membar_enter.

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


# 1.122 31-Mar-2022 pgoyette

For device modules that provide both auto-config and /dev/xxx
interfaces, make sure that initialization and destruction
follow the proper sequence. This is triggered by the recent
changes to the devsw stuff; per riastradh@ the required call
sequence is:

devsw_attach()
config_init_component() or config_cf*_attach()
...
config_fini_component() or config_cf*_detach()
devsw_detach()

While here, add a few missing calls to some of the detach
routines.

Testing of these changes has been limited to:
1. compile without build break
2. no related test failures from atf
3. modload/modunload work as well as
before.

No functional device testing done, since I don't have any
of these devices. Let me know of any damage I might cause
here!

XXX Some of the modules affected by this commit are already
XXX broken; see kern/56772. This commit does not break
any additional modules (as far as I know).


# 1.121 28-Mar-2022 riastradh

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


# 1.120 26-Mar-2022 isaki

Add terminology comments.


# 1.119 26-Mar-2022 isaki

Improve comments.


# 1.118 26-Mar-2022 isaki

Remove a dead code in audio_track_record().


# 1.117 26-Mar-2022 isaki

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.


# 1.116 26-Mar-2022 isaki

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.


# 1.115 14-Mar-2022 riastradh

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

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


# 1.114 14-Mar-2022 riastradh

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.


# 1.113 12-Dec-2021 andvar

fix various typos, mainly in comments.


# 1.112 10-Dec-2021 andvar

s/occured/occurred/ in comments, log messages and man pages.


# 1.111 05-Dec-2021 msaitoh

s/faciliate/facilitate/ in comment.


# 1.110 10-Oct-2021 riastradh

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!


# 1.109 10-Oct-2021 riastradh

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.


# 1.108 26-Sep-2021 thorpej

Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.128 21-Apr-2022 macallan

#if 0 now unused audio_track_is_record() to appease clang


# 1.127 20-Apr-2022 isaki

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.


# 1.126 20-Apr-2022 isaki

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).


# 1.125 20-Apr-2022 isaki

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


# 1.124 19-Apr-2022 riastradh

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+31d2619e72c2c8436cc9@syzkaller.appspotmail.com


# 1.123 09-Apr-2022 riastradh

audio(4): Use membar_acquire, not membar_enter.

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


# 1.122 31-Mar-2022 pgoyette

For device modules that provide both auto-config and /dev/xxx
interfaces, make sure that initialization and destruction
follow the proper sequence. This is triggered by the recent
changes to the devsw stuff; per riastradh@ the required call
sequence is:

devsw_attach()
config_init_component() or config_cf*_attach()
...
config_fini_component() or config_cf*_detach()
devsw_detach()

While here, add a few missing calls to some of the detach
routines.

Testing of these changes has been limited to:
1. compile without build break
2. no related test failures from atf
3. modload/modunload work as well as
before.

No functional device testing done, since I don't have any
of these devices. Let me know of any damage I might cause
here!

XXX Some of the modules affected by this commit are already
XXX broken; see kern/56772. This commit does not break
any additional modules (as far as I know).


# 1.121 28-Mar-2022 riastradh

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


# 1.120 26-Mar-2022 isaki

Add terminology comments.


# 1.119 26-Mar-2022 isaki

Improve comments.


# 1.118 26-Mar-2022 isaki

Remove a dead code in audio_track_record().


# 1.117 26-Mar-2022 isaki

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.


# 1.116 26-Mar-2022 isaki

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.


# 1.115 14-Mar-2022 riastradh

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

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


# 1.114 14-Mar-2022 riastradh

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.


# 1.113 12-Dec-2021 andvar

fix various typos, mainly in comments.


# 1.112 10-Dec-2021 andvar

s/occured/occurred/ in comments, log messages and man pages.


# 1.111 05-Dec-2021 msaitoh

s/faciliate/facilitate/ in comment.


# 1.110 10-Oct-2021 riastradh

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!


# 1.109 10-Oct-2021 riastradh

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.


# 1.108 26-Sep-2021 thorpej

Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.127 20-Apr-2022 isaki

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.


# 1.126 20-Apr-2022 isaki

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).


# 1.125 20-Apr-2022 isaki

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


# 1.124 19-Apr-2022 riastradh

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+31d2619e72c2c8436cc9@syzkaller.appspotmail.com


# 1.123 09-Apr-2022 riastradh

audio(4): Use membar_acquire, not membar_enter.

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


# 1.122 31-Mar-2022 pgoyette

For device modules that provide both auto-config and /dev/xxx
interfaces, make sure that initialization and destruction
follow the proper sequence. This is triggered by the recent
changes to the devsw stuff; per riastradh@ the required call
sequence is:

devsw_attach()
config_init_component() or config_cf*_attach()
...
config_fini_component() or config_cf*_detach()
devsw_detach()

While here, add a few missing calls to some of the detach
routines.

Testing of these changes has been limited to:
1. compile without build break
2. no related test failures from atf
3. modload/modunload work as well as
before.

No functional device testing done, since I don't have any
of these devices. Let me know of any damage I might cause
here!

XXX Some of the modules affected by this commit are already
XXX broken; see kern/56772. This commit does not break
any additional modules (as far as I know).


# 1.121 28-Mar-2022 riastradh

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


# 1.120 26-Mar-2022 isaki

Add terminology comments.


# 1.119 26-Mar-2022 isaki

Improve comments.


# 1.118 26-Mar-2022 isaki

Remove a dead code in audio_track_record().


# 1.117 26-Mar-2022 isaki

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.


# 1.116 26-Mar-2022 isaki

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.


# 1.115 14-Mar-2022 riastradh

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

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


# 1.114 14-Mar-2022 riastradh

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.


# 1.113 12-Dec-2021 andvar

fix various typos, mainly in comments.


# 1.112 10-Dec-2021 andvar

s/occured/occurred/ in comments, log messages and man pages.


# 1.111 05-Dec-2021 msaitoh

s/faciliate/facilitate/ in comment.


# 1.110 10-Oct-2021 riastradh

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!


# 1.109 10-Oct-2021 riastradh

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.


# 1.108 26-Sep-2021 thorpej

Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.124 19-Apr-2022 riastradh

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+31d2619e72c2c8436cc9@syzkaller.appspotmail.com


# 1.123 09-Apr-2022 riastradh

audio(4): Use membar_acquire, not membar_enter.

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


# 1.122 31-Mar-2022 pgoyette

For device modules that provide both auto-config and /dev/xxx
interfaces, make sure that initialization and destruction
follow the proper sequence. This is triggered by the recent
changes to the devsw stuff; per riastradh@ the required call
sequence is:

devsw_attach()
config_init_component() or config_cf*_attach()
...
config_fini_component() or config_cf*_detach()
devsw_detach()

While here, add a few missing calls to some of the detach
routines.

Testing of these changes has been limited to:
1. compile without build break
2. no related test failures from atf
3. modload/modunload work as well as
before.

No functional device testing done, since I don't have any
of these devices. Let me know of any damage I might cause
here!

XXX Some of the modules affected by this commit are already
XXX broken; see kern/56772. This commit does not break
any additional modules (as far as I know).


# 1.121 28-Mar-2022 riastradh

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


# 1.120 26-Mar-2022 isaki

Add terminology comments.


# 1.119 26-Mar-2022 isaki

Improve comments.


# 1.118 26-Mar-2022 isaki

Remove a dead code in audio_track_record().


# 1.117 26-Mar-2022 isaki

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.


# 1.116 26-Mar-2022 isaki

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.


# 1.115 14-Mar-2022 riastradh

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

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


# 1.114 14-Mar-2022 riastradh

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.


# 1.113 12-Dec-2021 andvar

fix various typos, mainly in comments.


# 1.112 10-Dec-2021 andvar

s/occured/occurred/ in comments, log messages and man pages.


# 1.111 05-Dec-2021 msaitoh

s/faciliate/facilitate/ in comment.


# 1.110 10-Oct-2021 riastradh

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!


# 1.109 10-Oct-2021 riastradh

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.


# 1.108 26-Sep-2021 thorpej

Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.123 09-Apr-2022 riastradh

audio(4): Use membar_acquire, not membar_enter.

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


# 1.122 31-Mar-2022 pgoyette

For device modules that provide both auto-config and /dev/xxx
interfaces, make sure that initialization and destruction
follow the proper sequence. This is triggered by the recent
changes to the devsw stuff; per riastradh@ the required call
sequence is:

devsw_attach()
config_init_component() or config_cf*_attach()
...
config_fini_component() or config_cf*_detach()
devsw_detach()

While here, add a few missing calls to some of the detach
routines.

Testing of these changes has been limited to:
1. compile without build break
2. no related test failures from atf
3. modload/modunload work as well as
before.

No functional device testing done, since I don't have any
of these devices. Let me know of any damage I might cause
here!

XXX Some of the modules affected by this commit are already
XXX broken; see kern/56772. This commit does not break
any additional modules (as far as I know).


# 1.121 28-Mar-2022 riastradh

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


# 1.120 26-Mar-2022 isaki

Add terminology comments.


# 1.119 26-Mar-2022 isaki

Improve comments.


# 1.118 26-Mar-2022 isaki

Remove a dead code in audio_track_record().


# 1.117 26-Mar-2022 isaki

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.


# 1.116 26-Mar-2022 isaki

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.


# 1.115 14-Mar-2022 riastradh

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

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


# 1.114 14-Mar-2022 riastradh

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.


# 1.113 12-Dec-2021 andvar

fix various typos, mainly in comments.


# 1.112 10-Dec-2021 andvar

s/occured/occurred/ in comments, log messages and man pages.


# 1.111 05-Dec-2021 msaitoh

s/faciliate/facilitate/ in comment.


# 1.110 10-Oct-2021 riastradh

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!


# 1.109 10-Oct-2021 riastradh

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.


# 1.108 26-Sep-2021 thorpej

Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.122 31-Mar-2022 pgoyette

For device modules that provide both auto-config and /dev/xxx
interfaces, make sure that initialization and destruction
follow the proper sequence. This is triggered by the recent
changes to the devsw stuff; per riastradh@ the required call
sequence is:

devsw_attach()
config_init_component() or config_cf*_attach()
...
config_fini_component() or config_cf*_detach()
devsw_detach()

While here, add a few missing calls to some of the detach
routines.

Testing of these changes has been limited to:
1. compile without build break
2. no related test failures from atf
3. modload/modunload work as well as
before.

No functional device testing done, since I don't have any
of these devices. Let me know of any damage I might cause
here!

XXX Some of the modules affected by this commit are already
XXX broken; see kern/56772. This commit does not break
any additional modules (as far as I know).


# 1.121 28-Mar-2022 riastradh

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


# 1.120 26-Mar-2022 isaki

Add terminology comments.


# 1.119 26-Mar-2022 isaki

Improve comments.


# 1.118 26-Mar-2022 isaki

Remove a dead code in audio_track_record().


# 1.117 26-Mar-2022 isaki

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.


# 1.116 26-Mar-2022 isaki

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.


# 1.115 14-Mar-2022 riastradh

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

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


# 1.114 14-Mar-2022 riastradh

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.


# 1.113 12-Dec-2021 andvar

fix various typos, mainly in comments.


# 1.112 10-Dec-2021 andvar

s/occured/occurred/ in comments, log messages and man pages.


# 1.111 05-Dec-2021 msaitoh

s/faciliate/facilitate/ in comment.


# 1.110 10-Oct-2021 riastradh

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!


# 1.109 10-Oct-2021 riastradh

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.


# 1.108 26-Sep-2021 thorpej

Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.121 28-Mar-2022 riastradh

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


# 1.120 26-Mar-2022 isaki

Add terminology comments.


# 1.119 26-Mar-2022 isaki

Improve comments.


# 1.118 26-Mar-2022 isaki

Remove a dead code in audio_track_record().


# 1.117 26-Mar-2022 isaki

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.


# 1.116 26-Mar-2022 isaki

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.


# 1.115 14-Mar-2022 riastradh

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

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


# 1.114 14-Mar-2022 riastradh

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.


# 1.113 12-Dec-2021 andvar

fix various typos, mainly in comments.


# 1.112 10-Dec-2021 andvar

s/occured/occurred/ in comments, log messages and man pages.


# 1.111 05-Dec-2021 msaitoh

s/faciliate/facilitate/ in comment.


# 1.110 10-Oct-2021 riastradh

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!


# 1.109 10-Oct-2021 riastradh

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.


# 1.108 26-Sep-2021 thorpej

Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.120 26-Mar-2022 isaki

Add terminology comments.


# 1.119 26-Mar-2022 isaki

Improve comments.


# 1.118 26-Mar-2022 isaki

Remove a dead code in audio_track_record().


# 1.117 26-Mar-2022 isaki

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.


# 1.116 26-Mar-2022 isaki

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.


# 1.115 14-Mar-2022 riastradh

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

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


# 1.114 14-Mar-2022 riastradh

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.


# 1.113 12-Dec-2021 andvar

fix various typos, mainly in comments.


# 1.112 10-Dec-2021 andvar

s/occured/occurred/ in comments, log messages and man pages.


# 1.111 05-Dec-2021 msaitoh

s/faciliate/facilitate/ in comment.


# 1.110 10-Oct-2021 riastradh

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!


# 1.109 10-Oct-2021 riastradh

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.


# 1.108 26-Sep-2021 thorpej

Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.115 14-Mar-2022 riastradh

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

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


# 1.114 14-Mar-2022 riastradh

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.


# 1.113 12-Dec-2021 andvar

fix various typos, mainly in comments.


# 1.112 10-Dec-2021 andvar

s/occured/occurred/ in comments, log messages and man pages.


# 1.111 05-Dec-2021 msaitoh

s/faciliate/facilitate/ in comment.


# 1.110 10-Oct-2021 riastradh

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!


# 1.109 10-Oct-2021 riastradh

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.


# 1.108 26-Sep-2021 thorpej

Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.113 12-Dec-2021 andvar

fix various typos, mainly in comments.


# 1.112 10-Dec-2021 andvar

s/occured/occurred/ in comments, log messages and man pages.


# 1.111 05-Dec-2021 msaitoh

s/faciliate/facilitate/ in comment.


# 1.110 10-Oct-2021 riastradh

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!


# 1.109 10-Oct-2021 riastradh

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.


# 1.108 26-Sep-2021 thorpej

Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.112 10-Dec-2021 andvar

s/occured/occurred/ in comments, log messages and man pages.


# 1.111 05-Dec-2021 msaitoh

s/faciliate/facilitate/ in comment.


# 1.110 10-Oct-2021 riastradh

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!


# 1.109 10-Oct-2021 riastradh

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.


# 1.108 26-Sep-2021 thorpej

Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.111 05-Dec-2021 msaitoh

s/faciliate/facilitate/ in comment.


# 1.110 10-Oct-2021 riastradh

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!


# 1.109 10-Oct-2021 riastradh

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.


# 1.108 26-Sep-2021 thorpej

Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.110 10-Oct-2021 riastradh

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!


# 1.109 10-Oct-2021 riastradh

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.


# 1.108 26-Sep-2021 thorpej

Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.108 26-Sep-2021 thorpej

Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.107 07-Sep-2021 andvar

s/aquire/acquire/ in comments, also one typo fix acqure->acquire.


Revision tags: thorpej-i2c-spi-conf2-base
# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.106 07-Aug-2021 thorpej

Merge thorpej-cfargs2.


Revision tags: thorpej-futex2-base thorpej-cfargs2-base thorpej-i2c-spi-conf-base
# 1.105 21-Jul-2021 isaki

branches: 1.105.2;
AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.105 21-Jul-2021 isaki

AUDIO_SETINFO: fix a bug that the gain and the balance could not be set
at the same time. Fix PR kern/56308.


Revision tags: thorpej-i2c-spi-conf-base
# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.104 08-Jun-2021 riastradh

audio(4): Revert revision 1.96 and redo it another way.

Instead of refusing to open /dev/audioN for writes when the device
doesn't support playback, just refuse to issue writes.

Although it seems more sensible to me to reject writable opens early
on, and it seems Solaris does so, this makes querying device
properties a little trickier and is a change to the NetBSD semantics.


Revision tags: cjep_sun2x-base1
# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

branches: 1.102.2;
audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base thorpej-i2c-spi-conf-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.103 04-Jun-2021 riastradh

audio(4): When closing /dev/audioN, drain before removing from list.

Previously, in revision 1.100, I factored the SLIST_REMOVE out of
audio_unlink and audio_close up into audioclose since it is now used
by /dev/audio, /dev/audioctl, and /dev/mixer alike. But I didn't
realize that the order

1. audio_track_drain
2. SLIST_REMOVE from sc_files

was significant; it matters because audio_track_drain waits for
wakeups that are delivered by hardware interrupts only to files
listed in sc_files.

This also fixes a bug introduced with the audiobell -- it was missing
the SLIST_REMOVE altogether.

For now, duplicate the SLIST_REMOVE calls in a few more places --
this is suboptimal but I want to make sure the logic works before
factoring it all out to tidy up.


Revision tags: cjep_sun2x-base
# 1.102 01-Jun-2021 riastradh

audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base thorpej-i2c-spi-conf-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.102 01-Jun-2021 riastradh

audio(4): audio_unlink never fails, so make it return void.


# 1.101 01-Jun-2021 riastradh

audio(4): Insert mixers and audioctls into sc_files on open too.

This is necessary so that on detach we set file->dying before any
operations, particularly audioclose, try to acquire a psref by the
time the audio softc is gone.

Candidate fix for PR kern/56164.


# 1.100 01-Jun-2021 riastradh

audio(4): No need to set file->dying when closing the file.

No other file operations are possible by the time we get to
audioclose.


# 1.99 01-Jun-2021 riastradh

audio(4): Grab sc->sc_cred under sc->sc_lock before freeing.

Otherwise we may race with open, leaking a cred no longer in use and
freeing a cred still in use.


# 1.98 01-Jun-2021 riastradh

audio(4): Use kmem_alloc(sizeof(*p)) style.


# 1.97 01-Jun-2021 riastradh

audio(4): Make sure to return ENXIO if device is being detached.

Don't return 0 indicating successful open of an unusable device!


# 1.96 01-Jun-2021 riastradh

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.


Revision tags: cjep_staticlib_x-base1 cjep_staticlib_x-base thorpej-i2c-spi-conf-base
# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


# 1.92 24-Apr-2021 thorpej

branches: 1.92.2;
Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.95 02-May-2021 nia

audio: Only score hardware formats by channel count at count<=2

Scoring by channel count makes sense when you are using hardware that
supports mono and stereo formats. However, if your hardware also supports
surround formats, defaulting to those might be confusing. So, don't
prefer them.

Problem reported and fix tested by tcmart14 with a 2015 model Macbook Pro.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


Revision tags: thorpej-i2c-spi-conf-base
# 1.92 24-Apr-2021 thorpej

Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.94 26-Apr-2021 thorpej

audioprint(): handle AUDIODEV_TYPE_AUX.


# 1.93 26-Apr-2021 thorpej

audio_attach_mi(): Be explicit about using the "audiobus" interface attribute,
as the caller may be a device that carries more than one.


Revision tags: thorpej-i2c-spi-conf-base
# 1.92 24-Apr-2021 thorpej

Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.92 24-Apr-2021 thorpej

Merge thorpej-cfargs branch:

Simplify and make extensible the config_search() / config_found() /
config_attach() interfaces: rather than having different variants for
which arguments you want pass along, just have a single call that
takes a variadic list of tag-value arguments.

Adjust all call sites:
- Simplify wherever possible; don't pass along arguments that aren't
actually needed.
- Don't be explicit about what interface attribute is attaching if
the device only has one. (More simplification.)
- Add a config_probe() function to be used in indirect configuiration
situations, making is visibly easier to see when indirect config is
in play, and allowing for future change in semantics. (As of now,
this is just a wrapper around config_match(), but that is an
implementation detail.)

Remove unnecessary or redundant interface attributes where they're not
needed.

There are currently 5 "cfargs" defined:
- CFARG_SUBMATCH (submatch function for direct config)
- CFARG_SEARCH (search function for indirect config)
- CFARG_IATTR (interface attribte)
- CFARG_LOCATORS (locators array)
- CFARG_DEVHANDLE (devhandle_t - wraps OFW, ACPI, etc. handles)

...and a sentinel value CFARG_EOL.

Add some extra sanity checking to ensure that interface attributes
aren't ambiguous.

Use CFARG_DEVHANDLE in MI FDT, OFW, and ACPI code, and macppc and shark
ports to associate those device handles with device_t instance. This
will trickle trough to more places over time (need back-end for pre-OFW
Sun OBP; any others?).


Revision tags: thorpej-cfargs-base thorpej-futex-base
# 1.91 14-Feb-2021 isaki

branches: 1.91.2;
Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.91 14-Feb-2021 isaki

Add missing curlwp_bindx() corresponding to curlwp_bind().
Pointed out by riastradh@.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


Revision tags: thorpej-futex-base
# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


Revision tags: thorpej-futex-base
# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.90 09-Feb-2021 isaki

Protect also audioopen() and audiobellopen() from audiodetach() with
psref(9), as well as others(audioread, audiowrite, etc..).
- Rename audio_file_enter to audio_sc_acquire_fromfile, audio_file_exit
to audio_sc_release, for clarify. These are the reference counter for
this sc.
- Introduce audio_sc_acquire_foropen for audio{,bell}open.
- audio_open needs to examine sc_dying again before inserting it into
sc_files, in order to keep sc_files consistency.
The race between audiodetach and audioopen is pointed out by riastradh@.
Thank you for many advices.


# 1.89 09-Feb-2021 isaki

Change the lock conditions to call audio_unlink().
This can remove a different copy of audio_exlock_enter() in audio_unlink()
and can use normal one. Also, in audiodetach(), this can set the exlock
at more natual order (before calling audio_unlink()).
No noticeable functional changes are intended.
Thanks for comments, riastradh@.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


Revision tags: thorpej-futex-base
# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.88 15-Jan-2021 isaki

Improve error messages.
- prefix MD device name if it's considered to be related to the MD driver.
- revise some messages.


# 1.87 15-Jan-2021 isaki

Change (harmless) zero-length debug messages.
sys/modules is compiled with -Wzero-length-format and this
makes sys/modules compilable even if AUDIO_DEBUG is defined.


Revision tags: thorpej-futex-base
# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.86 19-Dec-2020 thorpej

Use sel{record,remove}_knote().


Revision tags: thorpej-futex-base
# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


# 1.79 07-Sep-2020 isaki

branches: 1.79.2;
Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.85 13-Dec-2020 isaki

Revise comments.


# 1.84 13-Dec-2020 isaki

Fix a return value of audiopoll().
fo_poll is expected to return revents rather than errno on error.


# 1.83 13-Dec-2020 isaki

Avoid a dead lock in audiodetach, since rev 1.63.
audio_unlink() must be called without exlock held (and
audio_mixer_destroy() must be called with exlock held).
This makes unplugging during playing/recording work (again).
Reported by Julian Coleman on current-users:
http://mail-index.netbsd.org/current-users/2020/12/10/msg040050.html


# 1.82 13-Dec-2020 isaki

Add missing newline.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


Revision tags: thorpej-futex-base
# 1.79 07-Sep-2020 isaki

Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.81 09-Dec-2020 isaki

Rewrite error handling on audio_open().
This also fixes a few resource leaks on error case.


# 1.80 09-Dec-2020 isaki

Fix that audio_open() didn't halt the recording mixer correctly
if fd_allocfile() failed, since rev 1.65.
Will fix PR kern/55848.


Revision tags: thorpej-futex-base
# 1.79 07-Sep-2020 isaki

Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.79 07-Sep-2020 isaki

Fix misspellings in comment.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.78 23-Aug-2020 isaki

Improve debug messages.


# 1.77 23-Aug-2020 isaki

Improve and update comments.


# 1.76 23-Aug-2020 isaki

Style fixes.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.75 29-May-2020 isaki

Fix suspend/resume.
- Revert temporary usage of sc_[pr]busy during suspend. These indicate
whether the mixer needs to be restarted or not.
- Avoid timeout error when about to suspend.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.74 26-May-2020 nia

audio: Only restart recording mixer on resume if it's already been started


# 1.73 26-May-2020 nia

audio: remove comment that is no longer valid


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.72 26-May-2020 nia

audio: Restore compat with Solaris for 8-bit LINEAR.

It's always signed, unless it's LINEAR8.

PR kern/55175


# 1.71 26-May-2020 nia

audio: Fix logic for resuming when the device is in use.

audio_[r/p]mixer_start should never be called when the device is
marked busy.

Resolves a panic on resume when audio is playing, PR kern/55301


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.70 23-May-2020 ad

Move proc_lock into the data segment. It was dynamically allocated because
at the time we had mutex_obj_alloc() but not __cacheline_aligned.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.69 01-May-2020 isaki

Move machine dependent AUDIO_BLK_MS default value to <machine/param.h>.
If the port has __AUDIO_BLK_MS in <machine/param.h>, it will be used.
Otherwise the default value (currently 10 msec) defined in audio.c will
be used. This mechanism is for very old ports which cannot satisfactorily
handle 10 msec block. Currently hppa, m68k, sh3, sparc(!64) and vax are.

For port maintainers, if general models in your port cannot satisfactorily
handle 10 msec block, please consider to define your suitable longer period
(40 msec would be a good first choice).
But please don't be eager to make the default value shorter.

<machine/param.h> was discussed in source-changes-d. It's better than
ifdef storm, or adding 60+ new header files in every arch/*/include/
directories for this. Thanks mrg@, ad@, and everyone.
http://mail-index.netbsd.org/source-changes-d/2020/05/01/msg012572.html


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.68 29-Apr-2020 isaki

Set AUDIO_BLK_MS 40 msec on other old(slow) architectures not only m68k.
Thanks tsutsui@ for comment about architecture choice.
And move it from audiodef.h to audio.c as suggested by joerg@.


Revision tags: bouyer-xenpvh-base2 phil-wifi-20200421 bouyer-xenpvh-base1
# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

branches: 1.65.2;
Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.67 19-Apr-2020 isaki

Improve the blocksize notation.
The blocksize is expressed in bytes, and the millisecond notation
is supplementary information to make it easier to understand.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.66 17-Apr-2020 isaki

Improve diagnostic messages.


Revision tags: phil-wifi-20200411 bouyer-xenpvh-base phil-wifi-20200406
# 1.65 26-Mar-2020 isaki

Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


Revision tags: is-mlppp-base
# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.65 26-Mar-2020 isaki

Fix to start recording immediately when open() with READ mode is
called (unless pause).
- Opening /dev/audio always initializes pause with false. Therefore
it always starts recording.
- Opening /dev/sound inherites pause from the sticky parameter.
Therefore whether /dev/sound starts recording or not depends on the
sticky pause parameter.
This fixes two problems:
- Opening /dev/audio didn't start recording after merging isaki-audio2.
- Opening /dev/sound didn't start recording regardless of the sticky
pause, probably since long time ago (at least netbsd-7).


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.64 07-Mar-2020 isaki

Fix/Update comments about allocm/freem.


# 1.63 07-Mar-2020 isaki

Split sc_lock and sc_exlock.
Most (probably all) malloc/free (or routines which may sleep) now can be
called without holding mutex.
Pointed out by riastradh@.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.62 04-Mar-2020 isaki

Restore backward compatibility with netbsd-7 audio.
For sticky parameters (encoding, precision, channels, sample_rate and pause):
- AUDIO_SETINFO for nonexistent track updates sticky parameters.
- AUDIO_GETINFO for nonexistent track reads sticky parameters.
For blocksize, hiwat, lowat and {play.record}.buffer_size:
- AUDIO_SETINFO for nonexistent track does nothing.
- AUDIO_GETINFO for nonexistent track returns dummy non-zero values.
Nonexistent track is a playback track on O_RDONLY descriptor for example,
or both tracks on /dev/audioctl.


# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.61 01-Mar-2020 isaki

Improve an error message about round_blocksize.


# 1.60 01-Mar-2020 isaki

Fix wrong parameter displayed in debug messages.


# 1.59 01-Mar-2020 isaki

Reinitialize the sticky parameters whenever the hardware format is changed.
When the number of the hardware channels becomes less than the number of
channels that sticky parameters remember, subsequent open("/dev/sound") will
fail without this treatment. This is for rev 1.43.


Revision tags: ad-namecache-base3
# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.58 29-Feb-2020 isaki

Minor fix. audio_prinfo.pause is u_char, not bool.


# 1.57 29-Feb-2020 isaki

Release memories on audiobellclose.
It's rest of the last commit.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.56 23-Feb-2020 isaki

Prevent a race between audiodetach and fileops methods using psref(9).
Fix PR kern/54427.
Thank you so much riastradh@


# 1.55 23-Feb-2020 isaki

Improve error handling around audio_hw_probe().
It was difficult to return multiple errors.


# 1.54 23-Feb-2020 isaki

Make start_input/halt_input optional if the driver has no recording,
make start_output/halt_output optional if the driver has no playback.
And remove such never called functions.


# 1.53 22-Feb-2020 chs

remove some unnecessary includes of internal UVM headers.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.52 22-Feb-2020 isaki

Make calling get_props() lockless.
get_props() of all MD drivers now can be called without sc_lock.


# 1.51 22-Feb-2020 isaki

On audio_[pr]mixer_halt(), it's better to reset parameters in intr_lock.


# 1.50 22-Feb-2020 isaki

Add KASSERTs. audio_[pr]mixer_start() need exlock.


# 1.49 22-Feb-2020 isaki

Move #if DIAGNOSTIC..#endif to correct place.
It should ignore stray interrupts regardless of DIAGNOSTIC.


# 1.48 22-Feb-2020 isaki

hw_if->query_format is already mandatory method. Drop null checks.


# 1.47 22-Feb-2020 isaki

Improve KASSERT messages.


# 1.46 22-Feb-2020 isaki

Change two aprint_error_dev() to device_printf() (and improve messages).
This is also called from other than boot.


# 1.45 22-Feb-2020 isaki

const-ify. These arguments are no longer written back.


# 1.44 22-Feb-2020 isaki

Fix/Update comments.


# 1.43 22-Feb-2020 isaki

Limit the number of channels that userland apps can set (by AUDIO_SETINFO)
to the number of channels supported by the hardware or less, if the hardware
supports multi channels.
- On monaural or stereo hardware, userland apps can always set 1ch or 2ch.
The kernel (audio layer) can convert mono-stereo each other.
- On 3ch (2.1ch) hardware, for example, userland apps can set 1, 2, or 3ch,
but not 4ch or more.
This allows userland apps to know actual number of channels supported by
the hardware in the same way as before.
PR kern/54973.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.42 15-Feb-2020 isaki

Remove incorrect KASSERT(!mutex_owned()).
Pointed out by riastradh@.


Revision tags: ad-namecache-base2 ad-namecache-base1
# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

branches: 1.37.2;
Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.41 11-Jan-2020 isaki

Simplify async_mixer handling.
- It makes FIOASYNC code in mixer_ioctl() symmetric.
- For readability, mixer_async_{add,remove}() should take pid argument
though pid is always curproc.


# 1.40 11-Jan-2020 isaki

Remove old debug #ifdefs.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.39 08-Jan-2020 isaki

Fix an resource leak on audiobell close.
audioclose() freed audio_file_t structure, but only audiobellclose
didn't pass there. I change that all of freeing audio_file_t is done
by each *_close().


# 1.38 08-Jan-2020 isaki

Remove obsoleted comment.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


Revision tags: ad-namecache-base
# 1.37 08-Jan-2020 isaki

Move mutex_exit() correct place to protect sc_async_mixer.
Thanks maxv@!


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.36 27-Dec-2019 msaitoh

s/sucess/success/ in comment.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.35 26-Dec-2019 isaki

Improve and simplify around audio_realloc().


# 1.34 26-Dec-2019 isaki

Use M_WAITOK instead of M_NOWAIT.
These allocations don't require NOWAIT constraints.
Will fix PR kern/54796.


Revision tags: phil-wifi-20191119
# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.33 06-Nov-2019 isaki

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


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.32 18-Oct-2019 msaitoh

s/initalize/initialize/ in comment or printf message.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

branches: 1.28.2;
Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.31 06-Sep-2019 isaki

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


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.30 29-Aug-2019 isaki

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


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.29 23-Aug-2019 maxv

Add missing mutex, we were hitting a KASSERT.


Revision tags: netbsd-9-base
# 1.28 10-Jul-2019 isaki

Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.28 10-Jul-2019 isaki

Use kmem_alloc/free() instead of old kern_malloc/free().


# 1.27 10-Jul-2019 isaki

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


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.26 07-Jul-2019 isaki

Avoid memory reallocation on SET_FORMAT.


# 1.25 07-Jul-2019 isaki

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


# 1.24 07-Jul-2019 isaki

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.


# 1.23 06-Jul-2019 isaki

Implement auto recovery of the mixing volume.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.22 26-Jun-2019 isaki

Improve debug message and remove duplicated one.


# 1.21 26-Jun-2019 isaki

Improve audiobell (and interfaces with audio).
- Generate pseudo sine wave if possible. It may improve timbre.
If it cannot represent a sine wave, it falls back to a triangular
wave or a rectangular wave.
- Volume adjustment.
- Calculate playback frequency based on mixer frequency.
Now audiobellopen() initializes playback parameters other than
sample_rate, and new audiobellsetrate() sets sample_rate.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.20 25-Jun-2019 isaki

Put the lowered mixing volume back when all playback tracks are closed.
TODO: about long life descriptor.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.19 23-Jun-2019 isaki

Restore sysctl_teardown which was unintentionally dropped.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.18 19-Jun-2019 isaki

Don't wait until a block is filled.
This improves that short sound breaks.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.17 12-Jun-2019 isaki

Fill silence if the last block of the first track is not filled.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.16 10-Jun-2019 isaki

branches: 1.16.2;
Use AUDIO_SCALEDOWN() macro rather than seemingly strange ifdefs.
Discussed on source-changes-d.


# 1.15 10-Jun-2019 isaki

Add missing newline to some error messages.


# 1.14 10-Jun-2019 isaki

Call get_props() once at attach.


Revision tags: phil-wifi-20190609
# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.13 08-Jun-2019 isaki

Improve checking and displaying property.
- Full/half duplex and independentness are valid only on bidirectional
device.
- MMAP is no longer hardware driver's property.


# 1.12 08-Jun-2019 isaki

Clean get_props().
- Make get_props() return AUDIO_PROP_{PLAYBACK,CAPTURE} properly.
This eliminates need for audio.c to take care of such (old)
drivers which don't return both of PLAYBACK and CAPTURE.
- All get_props() doesn't need to return AUDIO_PROP_MMAP.
It is handled in the audio layer now.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.11 06-Jun-2019 isaki

Revert rev1.7.
Don't take care of incorrect drivers. They should be fixed.
PR kern/54264. OK'ed by nakayama@-san.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.10 25-May-2019 isaki

Fix typo in comment.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.9 23-May-2019 isaki

Remove unnecessary file lock.
It has been introduced to prevent multiple syscalls entering
simultaneously. But it's completely unnecessary.
It fixes firefox problem in PR kern/54177.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.8 21-May-2019 isaki

Adjust debug message. Inspired from PR kern/54177.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.7 13-May-2019 nakayama

audio_open:
On full duplex hardware, the flags passed to hw_if->open() is always
(FREAD | FWRITE). But some devices (e.g. uaudio) check its
capabilities at open(), so unidirection devices like USB speakers
always return EACCES.

Avoid open() failure on such devices by checking the capabilities of
the device and changing the flags passed to hw_if->open().


# 1.6 13-May-2019 nakayama

sysctl "multiuser" type is CTLTYPE_BOOL, so use bool instead of int.


# 1.5 13-May-2019 nakayama

audio_hw_probe:
Return error if both play and record probes failed.

Avoid audio is disabled on devices with only play like USB speakers.


# 1.4 13-May-2019 nakayama

audioattach:
Output the cause of the audio device disabled along with the error code.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.


# 1.3 11-May-2019 maya

Don't use a confusing GCC extension. NFC


# 1.2 08-May-2019 isaki

Merge isaki-audio2 branch, the overhaul of audio subsystem.
- Interrupt-oriented system rather than thread-oriented.
- Improve stability, quality and performance.
- Split playback and record cleanly. Improve halfduplex support.
- Many bugs are fixed including deadlocks, resource leaks, abuses, etc.
- Simplify audio filter mechanism. The encoding/channels/frequency
conversions are completely handled in the upper layer. So the hard-
ware driver only converts its hardware encoding (if necessary).
- audio_hw_if changes:
- Obsoletes query_encoding and add query_format instead.
- Obsoletes set_params and add set_format instead.
- Remove drain, setfd, mappage.
- The call sequences are changed.
- ioctl AUDIO_GETFD/SETFD, AUDIO_GETCHAN/SETCHAN are obsoleted.
- ioctl AUDIO_{QUERY,GET,SET}FORMAT are introduced.
- cleanup config attributes: au*conv and mulaw.
- All hardware drivers should follow it (I've done as much as possible).

Some file paths are changed:
- dev/audio.c -> dev/audio/audio.c (rewritten)
- dev/audiovar.h -> dev/audio/audiovar.h
- dev/audio_dai.h -> dev/audio/audio_dai.h
- dev/audio_if.h -> dev/audio/audio_if.h
- dev/audiobell.c -> dev/audio/audiobell.c
- dev/audiobellvar.h -> dev/audio/audiobellvar.h
- dev/mulaw.[ch] -> dev/audio/mulaw.[ch] + dev/audio/alaw.c


# 1.1 21-Apr-2019 isaki

branches: 1.1.2;
file audio.c was initially added on branch isaki-audio2.