sound: Simplify pcm_chnalloc() and fix infinite loop bug
Simplify logic to execute the following algorithm:
- Search for a free (i.e not busy) channel and return successfully.
- If no channel was found, either return an ENOTSUP if no VCHAN can be created (disabled or we reached the limit), or create one more VCHAN and scan for it again.
- If the second scan failed again, return EBUSY.
This patch also solves a bug where we'd end up in an infinite loop,
calling vchan_setnew() with the same newcnt value indefinitely, after
the following scenario:
- Plug device.
- Spawn X channels.
- Kill all channels except _last_ opened.
- Clean up all unused VCHANs.
- Spawn X+1 channels.
- Infinite loop in pcm_chnalloc().
I am not exactly sure which part of pcm_chnalloc() caused this bug, but
the patch fixes it at least...
Sponsored by: The FreeBSD Foundation
MFC after: 2 days
Reviewed by: dev_submerge.ch
Differential Revision: https://reviews.freebsd.org/D46548
(cherry picked from commit b973a14d354f332a3428cbab8e9d4b72cb3e2d6e)