2019-05-27 06:55:05 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2010-03-04 18:46:13 +00:00
|
|
|
/*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/init.h>
|
2010-03-29 08:01:48 +00:00
|
|
|
#include <linux/slab.h>
|
2013-04-16 16:01:39 +00:00
|
|
|
#include <linux/bitrev.h>
|
2012-04-12 11:51:12 +00:00
|
|
|
#include <linux/ratelimit.h>
|
2010-03-04 18:46:13 +00:00
|
|
|
#include <linux/usb.h>
|
|
|
|
#include <linux/usb/audio.h>
|
2010-03-11 20:13:20 +00:00
|
|
|
#include <linux/usb/audio-v2.h>
|
2010-03-04 18:46:13 +00:00
|
|
|
|
|
|
|
#include <sound/core.h>
|
|
|
|
#include <sound/pcm.h>
|
|
|
|
#include <sound/pcm_params.h>
|
|
|
|
|
|
|
|
#include "usbaudio.h"
|
|
|
|
#include "card.h"
|
|
|
|
#include "quirks.h"
|
|
|
|
#include "debug.h"
|
2011-09-14 10:46:57 +00:00
|
|
|
#include "endpoint.h"
|
2010-03-04 18:46:13 +00:00
|
|
|
#include "helper.h"
|
|
|
|
#include "pcm.h"
|
2010-05-31 12:51:31 +00:00
|
|
|
#include "clock.h"
|
2011-03-11 13:51:12 +00:00
|
|
|
#include "power.h"
|
media: sound/usb: Use Media Controller API to share media resources
Media Device Allocator API to allows multiple drivers share a media device.
This API solves a very common use-case for media devices where one physical
device (an USB stick) provides both audio and video. When such media device
exposes a standard USB Audio class, a proprietary Video class, two or more
independent drivers will share a single physical USB bridge. In such cases,
it is necessary to coordinate access to the shared resource.
Using this API, drivers can allocate a media device with the shared struct
device as the key. Once the media device is allocated by a driver, other
drivers can get a reference to it. The media device is released when all
the references are released.
Change the ALSA driver to use the Media Controller API to share media
resources with DVB, and V4L2 drivers on a AU0828 media device.
The Media Controller specific initialization is done after sound card is
registered. ALSA creates Media interface and entity function graph nodes
for Control, Mixer, PCM Playback, and PCM Capture devices.
snd_usb_hw_params() will call Media Controller enable source handler
interface to request the media resource. If resource request is granted,
it will release it from snd_usb_hw_free(). If resource is busy, -EBUSY is
returned.
Media specific cleanup is done in usb_audio_disconnect().
Reviewed-by: Takashi Iwai <tiwai@suse.de>
Signed-off-by: Shuah Khan <shuah@kernel.org>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
2019-04-02 00:40:22 +00:00
|
|
|
#include "media.h"
|
2010-03-04 18:46:13 +00:00
|
|
|
|
2012-04-12 11:51:12 +00:00
|
|
|
#define SUBSTREAM_FLAG_DATA_EP_STARTED 0
|
|
|
|
#define SUBSTREAM_FLAG_SYNC_EP_STARTED 1
|
|
|
|
|
2011-09-07 00:15:34 +00:00
|
|
|
/* return the estimated delay based on USB frame counters */
|
|
|
|
snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
|
|
|
|
unsigned int rate)
|
|
|
|
{
|
|
|
|
int current_frame_number;
|
|
|
|
int frame_diff;
|
|
|
|
int est_delay;
|
|
|
|
|
2012-11-23 15:00:37 +00:00
|
|
|
if (!subs->last_delay)
|
|
|
|
return 0; /* short path */
|
|
|
|
|
2011-09-07 00:15:34 +00:00
|
|
|
current_frame_number = usb_get_current_frame_number(subs->dev);
|
|
|
|
/*
|
|
|
|
* HCD implementations use different widths, use lower 8 bits.
|
|
|
|
* The delay will be managed up to 256ms, which is more than
|
|
|
|
* enough
|
|
|
|
*/
|
|
|
|
frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
|
|
|
|
|
|
|
|
/* Approximation based on number of samples per USB frame (ms),
|
|
|
|
some truncation for 44.1 but the estimate is good enough */
|
2012-12-19 17:39:05 +00:00
|
|
|
est_delay = frame_diff * rate / 1000;
|
|
|
|
if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
|
|
|
|
est_delay = subs->last_delay - est_delay;
|
|
|
|
else
|
|
|
|
est_delay = subs->last_delay + est_delay;
|
|
|
|
|
2011-09-07 00:15:34 +00:00
|
|
|
if (est_delay < 0)
|
|
|
|
est_delay = 0;
|
|
|
|
return est_delay;
|
|
|
|
}
|
|
|
|
|
2010-03-04 18:46:13 +00:00
|
|
|
/*
|
|
|
|
* return the current pcm pointer. just based on the hwptr_done value.
|
|
|
|
*/
|
|
|
|
static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
|
|
|
|
{
|
2018-05-27 13:09:15 +00:00
|
|
|
struct snd_usb_substream *subs = substream->runtime->private_data;
|
2010-03-04 18:46:13 +00:00
|
|
|
unsigned int hwptr_done;
|
|
|
|
|
ALSA: usb-audio: Avoid nested autoresume calls
After the recent fix of runtime PM for USB-audio driver, we got a
lockdep warning like:
=============================================
[ INFO: possible recursive locking detected ]
4.2.0-rc8+ #61 Not tainted
---------------------------------------------
pulseaudio/980 is trying to acquire lock:
(&chip->shutdown_rwsem){.+.+.+}, at: [<ffffffffa0355dac>] snd_usb_autoresume+0x1d/0x52 [snd_usb_audio]
but task is already holding lock:
(&chip->shutdown_rwsem){.+.+.+}, at: [<ffffffffa0355dac>] snd_usb_autoresume+0x1d/0x52 [snd_usb_audio]
This comes from snd_usb_autoresume() invoking down_read() and it's
used in a nested way. Although it's basically safe, per se (as these
are read locks), it's better to reduce such spurious warnings.
The read lock is needed to guarantee the execution of "shutdown"
(cleanup at disconnection) task after all concurrent tasks are
finished. This can be implemented in another better way.
Also, the current check of chip->in_pm isn't good enough for
protecting the racy execution of multiple auto-resumes.
This patch rewrites the logic of snd_usb_autoresume() & co; namely,
- The recursive call of autopm is avoided by the new refcount,
chip->active. The chip->in_pm flag is removed accordingly.
- Instead of rwsem, another refcount, chip->usage_count, is introduced
for tracking the period to delay the shutdown procedure. At
the last clear of this refcount, wake_up() to the shutdown waiter is
called.
- The shutdown flag is replaced with shutdown atomic count; this is
for reducing the lock.
- Two new helpers are introduced to simplify the management of these
refcounts; snd_usb_lock_shutdown() increases the usage_count, checks
the shutdown state, and does autoresume. snd_usb_unlock_shutdown()
does the opposite. Most of mixer and other codes just need this,
and simply returns an error if it receives an error from lock.
Fixes: 9003ebb13f61 ('ALSA: usb-audio: Fix runtime PM unbalance')
Reported-and-tested-by: Alexnader Kuleshov <kuleshovmail@gmail.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2015-08-25 14:09:00 +00:00
|
|
|
if (atomic_read(&subs->stream->chip->shutdown))
|
2012-10-12 13:12:55 +00:00
|
|
|
return SNDRV_PCM_POS_XRUN;
|
2010-03-04 18:46:13 +00:00
|
|
|
spin_lock(&subs->lock);
|
|
|
|
hwptr_done = subs->hwptr_done;
|
2012-12-19 17:39:05 +00:00
|
|
|
substream->runtime->delay = snd_usb_pcm_delay(subs,
|
2011-09-07 00:15:34 +00:00
|
|
|
substream->runtime->rate);
|
2010-03-04 18:46:13 +00:00
|
|
|
spin_unlock(&subs->lock);
|
|
|
|
return hwptr_done / (substream->runtime->frame_bits >> 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* find a matching audio format
|
|
|
|
*/
|
2012-09-18 16:49:48 +00:00
|
|
|
static struct audioformat *find_format(struct snd_usb_substream *subs)
|
2010-03-04 18:46:13 +00:00
|
|
|
{
|
2013-04-03 21:18:49 +00:00
|
|
|
struct audioformat *fp;
|
2010-03-04 18:46:13 +00:00
|
|
|
struct audioformat *found = NULL;
|
|
|
|
int cur_attr = 0, attr;
|
|
|
|
|
2013-04-03 21:18:49 +00:00
|
|
|
list_for_each_entry(fp, &subs->fmt_list, list) {
|
2013-04-22 23:00:41 +00:00
|
|
|
if (!(fp->formats & pcm_format_to_bits(subs->pcm_format)))
|
2010-03-04 18:46:15 +00:00
|
|
|
continue;
|
2012-09-18 16:49:48 +00:00
|
|
|
if (fp->channels != subs->channels)
|
2010-03-04 18:46:13 +00:00
|
|
|
continue;
|
2012-09-18 16:49:48 +00:00
|
|
|
if (subs->cur_rate < fp->rate_min ||
|
|
|
|
subs->cur_rate > fp->rate_max)
|
2010-03-04 18:46:13 +00:00
|
|
|
continue;
|
|
|
|
if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
|
|
|
|
unsigned int i;
|
|
|
|
for (i = 0; i < fp->nr_rates; i++)
|
2012-09-18 16:49:48 +00:00
|
|
|
if (fp->rate_table[i] == subs->cur_rate)
|
2010-03-04 18:46:13 +00:00
|
|
|
break;
|
|
|
|
if (i >= fp->nr_rates)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
|
|
|
|
if (! found) {
|
|
|
|
found = fp;
|
|
|
|
cur_attr = attr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* avoid async out and adaptive in if the other method
|
|
|
|
* supports the same format.
|
|
|
|
* this is a workaround for the case like
|
|
|
|
* M-audio audiophile USB.
|
|
|
|
*/
|
|
|
|
if (attr != cur_attr) {
|
|
|
|
if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
|
|
|
|
subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
|
|
|
|
(attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
|
|
|
|
subs->direction == SNDRV_PCM_STREAM_CAPTURE))
|
|
|
|
continue;
|
|
|
|
if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
|
|
|
|
subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
|
|
|
|
(cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
|
|
|
|
subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
|
|
|
|
found = fp;
|
|
|
|
cur_attr = attr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* find the format with the largest max. packet size */
|
|
|
|
if (fp->maxpacksize > found->maxpacksize) {
|
|
|
|
found = fp;
|
|
|
|
cur_attr = attr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2010-03-04 18:46:17 +00:00
|
|
|
static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
|
|
|
|
struct usb_host_interface *alts,
|
|
|
|
struct audioformat *fmt)
|
|
|
|
{
|
|
|
|
struct usb_device *dev = chip->dev;
|
|
|
|
unsigned int ep;
|
|
|
|
unsigned char data[1];
|
|
|
|
int err;
|
|
|
|
|
2016-03-15 14:20:58 +00:00
|
|
|
if (get_iface_desc(alts)->bNumEndpoints < 1)
|
|
|
|
return -EINVAL;
|
2010-03-04 18:46:17 +00:00
|
|
|
ep = get_endpoint(alts, 0)->bEndpointAddress;
|
|
|
|
|
|
|
|
data[0] = 1;
|
2018-05-27 13:18:22 +00:00
|
|
|
err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
|
|
|
|
USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
|
|
|
|
UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
|
|
|
|
data, sizeof(data));
|
|
|
|
if (err < 0) {
|
2014-02-26 12:02:17 +00:00
|
|
|
usb_audio_err(chip, "%d:%d: cannot set enable PITCH\n",
|
|
|
|
iface, ep);
|
2010-03-04 18:46:17 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2010-03-04 18:46:13 +00:00
|
|
|
|
2010-05-26 16:11:39 +00:00
|
|
|
static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
|
|
|
|
struct usb_host_interface *alts,
|
|
|
|
struct audioformat *fmt)
|
|
|
|
{
|
|
|
|
struct usb_device *dev = chip->dev;
|
|
|
|
unsigned char data[1];
|
|
|
|
int err;
|
|
|
|
|
|
|
|
data[0] = 1;
|
2018-05-27 13:18:22 +00:00
|
|
|
err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
|
|
|
|
USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
|
|
|
|
UAC2_EP_CS_PITCH << 8, 0,
|
|
|
|
data, sizeof(data));
|
|
|
|
if (err < 0) {
|
2014-02-26 12:02:17 +00:00
|
|
|
usb_audio_err(chip, "%d:%d: cannot set enable PITCH (v2)\n",
|
|
|
|
iface, fmt->altsetting);
|
2010-05-26 16:11:39 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-04 18:46:13 +00:00
|
|
|
/*
|
2010-05-26 16:11:39 +00:00
|
|
|
* initialize the pitch control and sample rate
|
2010-03-04 18:46:13 +00:00
|
|
|
*/
|
2010-03-04 18:46:17 +00:00
|
|
|
int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
|
2010-03-04 18:46:13 +00:00
|
|
|
struct usb_host_interface *alts,
|
|
|
|
struct audioformat *fmt)
|
|
|
|
{
|
2010-05-26 16:11:39 +00:00
|
|
|
/* if endpoint doesn't have pitch control, bail out */
|
|
|
|
if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
|
|
|
|
return 0;
|
|
|
|
|
2013-01-31 20:39:17 +00:00
|
|
|
switch (fmt->protocol) {
|
2010-03-04 18:46:17 +00:00
|
|
|
case UAC_VERSION_1:
|
2010-09-03 08:53:11 +00:00
|
|
|
default:
|
2010-03-04 18:46:17 +00:00
|
|
|
return init_pitch_v1(chip, iface, alts, fmt);
|
|
|
|
|
|
|
|
case UAC_VERSION_2:
|
2010-05-26 16:11:39 +00:00
|
|
|
return init_pitch_v2(chip, iface, alts, fmt);
|
2010-03-04 18:46:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 22:37:46 +00:00
|
|
|
static int start_endpoints(struct snd_usb_substream *subs)
|
2012-04-12 11:51:12 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!subs->data_endpoint)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
|
|
|
|
struct snd_usb_endpoint *ep = subs->data_endpoint;
|
|
|
|
|
2020-11-23 08:53:13 +00:00
|
|
|
dev_dbg(&subs->dev->dev, "Starting data EP 0x%x\n", ep->ep_num);
|
2012-04-12 11:51:12 +00:00
|
|
|
|
|
|
|
ep->data_subs = subs;
|
2017-01-04 22:37:46 +00:00
|
|
|
err = snd_usb_endpoint_start(ep);
|
2012-04-12 11:51:12 +00:00
|
|
|
if (err < 0) {
|
|
|
|
clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (subs->sync_endpoint &&
|
|
|
|
!test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
|
|
|
|
struct snd_usb_endpoint *ep = subs->sync_endpoint;
|
|
|
|
|
2020-11-23 08:53:13 +00:00
|
|
|
dev_dbg(&subs->dev->dev, "Starting sync EP 0x%x\n", ep->ep_num);
|
2012-04-12 11:51:12 +00:00
|
|
|
|
|
|
|
ep->sync_slave = subs->data_endpoint;
|
2017-01-04 22:37:46 +00:00
|
|
|
err = snd_usb_endpoint_start(ep);
|
2012-04-12 11:51:12 +00:00
|
|
|
if (err < 0) {
|
|
|
|
clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
|
2020-11-23 08:53:12 +00:00
|
|
|
ep->sync_slave = NULL;
|
2012-04-12 11:51:12 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-10 06:34:54 +00:00
|
|
|
static void sync_pending_stops(struct snd_usb_substream *subs)
|
|
|
|
{
|
|
|
|
snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
|
|
|
|
snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stop_endpoints(struct snd_usb_substream *subs)
|
2012-04-12 11:51:12 +00:00
|
|
|
{
|
2020-11-23 08:53:12 +00:00
|
|
|
if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
|
2020-11-23 08:53:13 +00:00
|
|
|
dev_dbg(&subs->dev->dev, "Stopping sync EP 0x%x\n",
|
|
|
|
subs->sync_endpoint->ep_num);
|
2012-11-21 07:30:48 +00:00
|
|
|
snd_usb_endpoint_stop(subs->sync_endpoint);
|
2020-11-23 08:53:12 +00:00
|
|
|
subs->sync_endpoint->sync_slave = NULL;
|
|
|
|
}
|
2012-04-12 11:51:12 +00:00
|
|
|
|
2020-11-23 08:53:13 +00:00
|
|
|
if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
|
|
|
|
dev_dbg(&subs->dev->dev, "Stopping data EP 0x%x\n",
|
|
|
|
subs->data_endpoint->ep_num);
|
2012-11-21 07:30:48 +00:00
|
|
|
snd_usb_endpoint_stop(subs->data_endpoint);
|
2020-11-23 08:53:13 +00:00
|
|
|
}
|
2019-12-10 06:34:54 +00:00
|
|
|
}
|
2012-11-21 07:30:48 +00:00
|
|
|
|
2019-12-10 06:34:54 +00:00
|
|
|
/* PCM sync_stop callback */
|
|
|
|
static int snd_usb_pcm_sync_stop(struct snd_pcm_substream *substream)
|
|
|
|
{
|
|
|
|
struct snd_usb_substream *subs = substream->runtime->private_data;
|
|
|
|
|
|
|
|
if (!snd_usb_lock_shutdown(subs->stream->chip)) {
|
|
|
|
sync_pending_stops(subs);
|
|
|
|
snd_usb_unlock_shutdown(subs->stream->chip);
|
2012-11-21 07:30:48 +00:00
|
|
|
}
|
2019-12-10 06:34:54 +00:00
|
|
|
return 0;
|
2012-04-12 11:51:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-23 08:53:10 +00:00
|
|
|
/* Check whether the given iface:altsetting points to an implicit fb source */
|
|
|
|
static bool search_generic_implicit_fb(struct usb_device *dev, int ifnum,
|
|
|
|
unsigned int altsetting,
|
|
|
|
struct usb_host_interface **altsp,
|
|
|
|
unsigned int *ep)
|
2013-02-03 21:31:20 +00:00
|
|
|
{
|
|
|
|
struct usb_interface *iface;
|
2020-11-23 08:53:10 +00:00
|
|
|
struct usb_host_interface *alts;
|
|
|
|
struct usb_interface_descriptor *altsd;
|
|
|
|
struct usb_endpoint_descriptor *epd;
|
|
|
|
|
|
|
|
iface = usb_ifnum_to_if(dev, ifnum);
|
|
|
|
if (!iface)
|
|
|
|
return false;
|
|
|
|
alts = usb_altnum_to_altsetting(iface, altsetting);
|
|
|
|
if (!alts)
|
|
|
|
return false;
|
|
|
|
altsd = get_iface_desc(alts);
|
|
|
|
if (altsd->bInterfaceClass != USB_CLASS_AUDIO ||
|
|
|
|
altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING ||
|
|
|
|
altsd->bInterfaceProtocol != UAC_VERSION_2 ||
|
|
|
|
altsd->bNumEndpoints < 1)
|
|
|
|
return false;
|
|
|
|
epd = get_endpoint(alts, 0);
|
|
|
|
if (!usb_endpoint_is_isoc_in(epd) ||
|
|
|
|
(epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
|
|
|
|
USB_ENDPOINT_USAGE_IMPLICIT_FB)
|
|
|
|
return false;
|
|
|
|
*ep = epd->bEndpointAddress;
|
|
|
|
*altsp = alts;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Like the function above, but specific to Roland with vendor class and hack */
|
|
|
|
static bool search_roland_implicit_fb(struct usb_device *dev, int ifnum,
|
|
|
|
unsigned int altsetting,
|
|
|
|
struct usb_host_interface **altsp,
|
|
|
|
unsigned int *ep)
|
|
|
|
{
|
|
|
|
struct usb_interface *iface;
|
|
|
|
struct usb_host_interface *alts;
|
2013-02-03 21:31:20 +00:00
|
|
|
struct usb_interface_descriptor *altsd;
|
|
|
|
struct usb_endpoint_descriptor *epd;
|
|
|
|
|
|
|
|
iface = usb_ifnum_to_if(dev, ifnum);
|
2020-11-23 08:53:10 +00:00
|
|
|
if (!iface)
|
|
|
|
return false;
|
|
|
|
alts = usb_altnum_to_altsetting(iface, altsetting);
|
|
|
|
if (!alts)
|
|
|
|
return false;
|
|
|
|
altsd = get_iface_desc(alts);
|
|
|
|
if (altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
|
2013-02-03 21:31:20 +00:00
|
|
|
(altsd->bInterfaceSubClass != 2 &&
|
2020-11-23 08:53:10 +00:00
|
|
|
altsd->bInterfaceProtocol != 2) ||
|
2013-02-03 21:31:20 +00:00
|
|
|
altsd->bNumEndpoints < 1)
|
2020-11-23 08:53:10 +00:00
|
|
|
return false;
|
|
|
|
epd = get_endpoint(alts, 0);
|
2013-02-03 21:31:20 +00:00
|
|
|
if (!usb_endpoint_is_isoc_in(epd) ||
|
|
|
|
(epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
|
|
|
|
USB_ENDPOINT_USAGE_IMPLICIT_FB)
|
2020-11-23 08:53:10 +00:00
|
|
|
return false;
|
2013-02-03 21:31:20 +00:00
|
|
|
*ep = epd->bEndpointAddress;
|
2020-11-23 08:53:10 +00:00
|
|
|
*altsp = alts;
|
|
|
|
return true;
|
2013-02-03 21:31:20 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 14:32:35 +00:00
|
|
|
/* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk
|
|
|
|
* applies. Returns 1 if a quirk was found.
|
|
|
|
*/
|
2020-11-23 08:53:14 +00:00
|
|
|
static int audioformat_implicit_fb_quirk(struct snd_usb_audio *chip,
|
|
|
|
struct audioformat *fmt,
|
|
|
|
struct usb_interface *iface,
|
|
|
|
struct usb_host_interface *alts)
|
2010-03-04 18:46:13 +00:00
|
|
|
{
|
2020-11-23 08:53:14 +00:00
|
|
|
struct usb_device *dev = chip->dev;
|
|
|
|
struct usb_interface_descriptor *altsd = get_iface_desc(alts);
|
|
|
|
unsigned int attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
|
2013-08-03 08:50:18 +00:00
|
|
|
unsigned int ep;
|
2018-04-18 14:35:34 +00:00
|
|
|
unsigned int ifnum;
|
2012-04-12 11:51:14 +00:00
|
|
|
|
2020-11-23 08:53:14 +00:00
|
|
|
switch (chip->usb_id) {
|
2012-11-28 22:55:41 +00:00
|
|
|
case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
|
2013-02-09 17:56:35 +00:00
|
|
|
case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
|
2020-11-04 11:57:17 +00:00
|
|
|
case USB_ID(0x22f0, 0x0006): /* Allen&Heath Qu-16 */
|
2013-08-03 08:50:21 +00:00
|
|
|
ep = 0x81;
|
2018-04-18 14:35:34 +00:00
|
|
|
ifnum = 3;
|
|
|
|
goto add_sync_ep_from_ifnum;
|
2012-04-12 11:51:14 +00:00
|
|
|
case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
|
|
|
|
case USB_ID(0x0763, 0x2081):
|
2013-08-03 08:50:21 +00:00
|
|
|
ep = 0x81;
|
2018-04-18 14:35:34 +00:00
|
|
|
ifnum = 2;
|
|
|
|
goto add_sync_ep_from_ifnum;
|
|
|
|
case USB_ID(0x2466, 0x8003): /* Fractal Audio Axe-Fx II */
|
2020-11-04 12:07:05 +00:00
|
|
|
case USB_ID(0x0499, 0x172a): /* Yamaha MODX */
|
2016-12-08 06:36:48 +00:00
|
|
|
ep = 0x86;
|
2018-04-18 14:35:34 +00:00
|
|
|
ifnum = 2;
|
|
|
|
goto add_sync_ep_from_ifnum;
|
2018-04-18 14:35:35 +00:00
|
|
|
case USB_ID(0x2466, 0x8010): /* Fractal Audio Axe-Fx III */
|
|
|
|
ep = 0x81;
|
|
|
|
ifnum = 2;
|
|
|
|
goto add_sync_ep_from_ifnum;
|
2020-10-26 05:05:47 +00:00
|
|
|
case USB_ID(0x1686, 0xf029): /* Zoom UAC-2 */
|
|
|
|
ep = 0x82;
|
|
|
|
ifnum = 2;
|
|
|
|
goto add_sync_ep_from_ifnum;
|
2019-08-20 06:58:12 +00:00
|
|
|
case USB_ID(0x1397, 0x0001): /* Behringer UFX1604 */
|
2018-04-18 14:35:34 +00:00
|
|
|
case USB_ID(0x1397, 0x0002): /* Behringer UFX1204 */
|
2018-02-09 14:51:36 +00:00
|
|
|
ep = 0x81;
|
2018-04-18 14:35:34 +00:00
|
|
|
ifnum = 1;
|
|
|
|
goto add_sync_ep_from_ifnum;
|
2020-02-29 15:18:15 +00:00
|
|
|
case USB_ID(0x07fd, 0x0004): /* MOTU MicroBook II/IIc */
|
|
|
|
/* MicroBook IIc */
|
|
|
|
if (altsd->bInterfaceClass == USB_CLASS_AUDIO)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* MicroBook II */
|
2019-02-28 19:34:04 +00:00
|
|
|
ep = 0x84;
|
|
|
|
ifnum = 0;
|
|
|
|
goto add_sync_ep_from_ifnum;
|
2020-01-15 15:13:58 +00:00
|
|
|
case USB_ID(0x07fd, 0x0008): /* MOTU M Series */
|
2020-06-21 07:50:05 +00:00
|
|
|
case USB_ID(0x31e9, 0x0001): /* Solid State Logic SSL2 */
|
2020-06-12 11:18:07 +00:00
|
|
|
case USB_ID(0x31e9, 0x0002): /* Solid State Logic SSL2+ */
|
2020-08-25 16:55:18 +00:00
|
|
|
case USB_ID(0x0499, 0x172f): /* Steinberg UR22C */
|
2020-07-03 10:04:33 +00:00
|
|
|
case USB_ID(0x0d9a, 0x00df): /* RTX6001 */
|
2020-01-15 15:13:58 +00:00
|
|
|
ep = 0x81;
|
|
|
|
ifnum = 2;
|
|
|
|
goto add_sync_ep_from_ifnum;
|
2020-06-01 10:22:24 +00:00
|
|
|
case USB_ID(0x2b73, 0x000a): /* Pioneer DJ DJM-900NXS2 */
|
2020-08-25 15:31:13 +00:00
|
|
|
case USB_ID(0x2b73, 0x0017): /* Pioneer DJ DJM-250MK2 */
|
2020-06-01 10:22:24 +00:00
|
|
|
ep = 0x82;
|
|
|
|
ifnum = 0;
|
|
|
|
goto add_sync_ep_from_ifnum;
|
2019-10-11 17:19:36 +00:00
|
|
|
case USB_ID(0x0582, 0x01d8): /* BOSS Katana */
|
|
|
|
/* BOSS Katana amplifiers do not need quirks */
|
|
|
|
return 0;
|
2012-04-12 11:51:14 +00:00
|
|
|
}
|
2018-04-18 14:35:34 +00:00
|
|
|
|
2020-11-23 08:53:10 +00:00
|
|
|
/* Generic UAC2 implicit feedback */
|
|
|
|
if (attr == USB_ENDPOINT_SYNC_ASYNC &&
|
|
|
|
altsd->bInterfaceClass == USB_CLASS_AUDIO &&
|
|
|
|
altsd->bInterfaceProtocol == UAC_VERSION_2 &&
|
|
|
|
altsd->bNumEndpoints == 1) {
|
|
|
|
ifnum = altsd->bInterfaceNumber + 1;
|
|
|
|
if (search_generic_implicit_fb(dev, ifnum,
|
|
|
|
altsd->bAlternateSetting,
|
|
|
|
&alts, &ep))
|
|
|
|
goto add_sync_ep;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Roland/BOSS implicit feedback with vendor spec class */
|
2013-08-03 08:50:21 +00:00
|
|
|
if (attr == USB_ENDPOINT_SYNC_ASYNC &&
|
2013-02-03 21:31:20 +00:00
|
|
|
altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
|
|
|
|
altsd->bInterfaceProtocol == 2 &&
|
|
|
|
altsd->bNumEndpoints == 1 &&
|
2020-11-23 08:53:14 +00:00
|
|
|
USB_ID_VENDOR(chip->usb_id) == 0x0582 /* Roland */) {
|
|
|
|
ifnum = altsd->bInterfaceNumber + 1;
|
|
|
|
if (search_roland_implicit_fb(dev, ifnum,
|
|
|
|
altsd->bAlternateSetting,
|
|
|
|
&alts, &ep))
|
|
|
|
goto add_sync_ep;
|
|
|
|
}
|
2012-04-12 11:51:12 +00:00
|
|
|
|
2013-08-03 08:50:18 +00:00
|
|
|
/* No quirk */
|
|
|
|
return 0;
|
|
|
|
|
2018-04-18 14:35:34 +00:00
|
|
|
add_sync_ep_from_ifnum:
|
|
|
|
iface = usb_ifnum_to_if(dev, ifnum);
|
|
|
|
|
2020-01-14 08:39:53 +00:00
|
|
|
if (!iface || iface->num_altsetting < 2)
|
2020-11-23 08:53:14 +00:00
|
|
|
return 0;
|
2018-04-18 14:35:34 +00:00
|
|
|
|
|
|
|
alts = &iface->altsetting[1];
|
|
|
|
|
2013-08-03 08:50:18 +00:00
|
|
|
add_sync_ep:
|
2020-11-23 08:53:14 +00:00
|
|
|
fmt->sync_ep = ep;
|
|
|
|
fmt->sync_iface = ifnum;
|
|
|
|
fmt->sync_altsetting = alts->desc.bAlternateSetting;
|
|
|
|
fmt->implicit_fb = 1;
|
|
|
|
dev_dbg(&dev->dev, "%d:%d: found implicit_fb sync_ep=%x, iface=%d, alt=%d\n",
|
|
|
|
fmt->iface, fmt->altsetting, fmt->sync_ep, fmt->sync_iface,
|
|
|
|
fmt->sync_altsetting);
|
2013-08-03 08:50:18 +00:00
|
|
|
|
2019-01-31 14:32:35 +00:00
|
|
|
return 1;
|
2013-08-03 08:50:18 +00:00
|
|
|
}
|
|
|
|
|
2020-11-23 08:53:14 +00:00
|
|
|
int snd_usb_audioformat_set_sync_ep(struct snd_usb_audio *chip,
|
|
|
|
struct audioformat *fmt)
|
2013-08-03 08:50:18 +00:00
|
|
|
{
|
2020-11-23 08:53:14 +00:00
|
|
|
struct usb_device *dev = chip->dev;
|
|
|
|
struct usb_interface *iface;
|
|
|
|
struct usb_host_interface *alts;
|
|
|
|
struct usb_interface_descriptor *altsd;
|
|
|
|
unsigned int ep, attr, sync_attr;
|
|
|
|
bool is_playback;
|
2013-08-03 08:50:18 +00:00
|
|
|
int err;
|
|
|
|
|
2020-11-23 08:53:14 +00:00
|
|
|
iface = usb_ifnum_to_if(dev, fmt->iface);
|
|
|
|
if (!iface)
|
|
|
|
return 0;
|
|
|
|
alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
|
|
|
|
if (!alts)
|
2019-01-31 14:32:35 +00:00
|
|
|
return 0;
|
2020-11-23 08:53:14 +00:00
|
|
|
altsd = get_iface_desc(alts);
|
|
|
|
|
|
|
|
is_playback = !(get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN);
|
|
|
|
if (is_playback) {
|
|
|
|
err = audioformat_implicit_fb_quirk(chip, fmt, iface, alts);
|
|
|
|
if (err > 0)
|
|
|
|
return 0;
|
|
|
|
}
|
2019-01-31 14:32:35 +00:00
|
|
|
|
2013-08-03 08:50:19 +00:00
|
|
|
if (altsd->bNumEndpoints < 2)
|
|
|
|
return 0;
|
2012-04-12 11:51:14 +00:00
|
|
|
|
2020-11-23 08:53:14 +00:00
|
|
|
attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
|
2015-08-14 22:19:43 +00:00
|
|
|
if ((is_playback && (attr == USB_ENDPOINT_SYNC_SYNC ||
|
|
|
|
attr == USB_ENDPOINT_SYNC_ADAPTIVE)) ||
|
2013-08-03 08:50:19 +00:00
|
|
|
(!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
|
|
|
|
return 0;
|
2012-04-12 11:51:12 +00:00
|
|
|
|
2020-11-23 08:53:14 +00:00
|
|
|
sync_attr = get_endpoint(alts, 1)->bmAttributes;
|
|
|
|
|
2015-08-14 22:19:43 +00:00
|
|
|
/*
|
|
|
|
* In case of illegal SYNC_NONE for OUT endpoint, we keep going to see
|
|
|
|
* if we don't find a sync endpoint, as on M-Audio Transit. In case of
|
|
|
|
* error fall back to SYNC mode and don't create sync endpoint
|
|
|
|
*/
|
|
|
|
|
2013-08-03 08:50:19 +00:00
|
|
|
/* check sync-pipe endpoint */
|
|
|
|
/* ... and check descriptor size before accessing bSynchAddress
|
|
|
|
because there is a version of the SB Audigy 2 NX firmware lacking
|
|
|
|
the audio fields in the endpoint descriptors */
|
2020-11-23 08:53:14 +00:00
|
|
|
if ((sync_attr & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
|
2013-08-03 08:50:19 +00:00
|
|
|
(get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
|
2013-08-03 08:50:20 +00:00
|
|
|
get_endpoint(alts, 1)->bSynchAddress != 0)) {
|
2014-02-26 12:02:17 +00:00
|
|
|
dev_err(&dev->dev,
|
|
|
|
"%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
|
|
|
|
fmt->iface, fmt->altsetting,
|
2013-08-03 08:50:19 +00:00
|
|
|
get_endpoint(alts, 1)->bmAttributes,
|
|
|
|
get_endpoint(alts, 1)->bLength,
|
|
|
|
get_endpoint(alts, 1)->bSynchAddress);
|
2015-08-14 22:19:43 +00:00
|
|
|
if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
|
|
|
|
return 0;
|
2013-08-03 08:50:19 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
ep = get_endpoint(alts, 1)->bEndpointAddress;
|
2013-08-03 08:50:20 +00:00
|
|
|
if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
|
2019-08-02 11:52:14 +00:00
|
|
|
get_endpoint(alts, 0)->bSynchAddress != 0 &&
|
2013-08-03 08:50:19 +00:00
|
|
|
((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
|
|
|
|
(!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
|
2014-02-26 12:02:17 +00:00
|
|
|
dev_err(&dev->dev,
|
|
|
|
"%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
|
|
|
|
fmt->iface, fmt->altsetting,
|
2013-08-03 08:50:19 +00:00
|
|
|
is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
|
2015-08-14 22:19:43 +00:00
|
|
|
if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
|
|
|
|
return 0;
|
2013-08-03 08:50:19 +00:00
|
|
|
return -EINVAL;
|
2012-04-12 11:51:12 +00:00
|
|
|
}
|
2010-03-04 18:46:13 +00:00
|
|
|
|
2020-11-23 08:53:14 +00:00
|
|
|
fmt->sync_ep = ep;
|
|
|
|
fmt->sync_iface = altsd->bInterfaceNumber;
|
|
|
|
fmt->sync_altsetting = altsd->bAlternateSetting;
|
|
|
|
if ((sync_attr & USB_ENDPOINT_USAGE_MASK) == USB_ENDPOINT_USAGE_IMPLICIT_FB)
|
|
|
|
fmt->implicit_fb = 1;
|
|
|
|
|
|
|
|
dev_dbg(&dev->dev, "%d:%d: found sync_ep=0x%x, iface=%d, alt=%d, implicit_fb=%d\n",
|
|
|
|
fmt->iface, fmt->altsetting, fmt->sync_ep, fmt->sync_iface,
|
|
|
|
fmt->sync_altsetting, fmt->implicit_fb);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int set_sync_endpoint(struct snd_usb_substream *subs,
|
|
|
|
struct audioformat *fmt)
|
|
|
|
{
|
|
|
|
struct usb_device *dev = subs->dev;
|
|
|
|
struct usb_interface *iface;
|
|
|
|
struct usb_host_interface *alts;
|
|
|
|
int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
|
|
|
|
unsigned int ep;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
subs->sync_endpoint = NULL;
|
|
|
|
subs->data_endpoint->sync_master = NULL;
|
|
|
|
|
|
|
|
ep = fmt->sync_ep;
|
|
|
|
if (!ep)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
iface = usb_ifnum_to_if(dev, fmt->sync_iface);
|
|
|
|
if (!iface)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
|
|
|
|
if (!alts)
|
|
|
|
return 0;
|
2013-08-03 08:50:19 +00:00
|
|
|
|
|
|
|
subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
|
|
|
|
alts, ep, !subs->direction,
|
2020-11-23 08:53:14 +00:00
|
|
|
fmt->implicit_fb ?
|
|
|
|
SND_USB_ENDPOINT_TYPE_DATA :
|
|
|
|
SND_USB_ENDPOINT_TYPE_SYNC);
|
2015-08-14 22:19:43 +00:00
|
|
|
if (!subs->sync_endpoint) {
|
2020-11-23 08:53:14 +00:00
|
|
|
if (is_playback &&
|
|
|
|
(fmt->ep_attr & USB_ENDPOINT_SYNCTYPE) == USB_ENDPOINT_SYNC_NONE)
|
2015-08-14 22:19:43 +00:00
|
|
|
return 0;
|
2013-08-03 08:50:19 +00:00
|
|
|
return -EINVAL;
|
2015-08-14 22:19:43 +00:00
|
|
|
}
|
2013-08-03 08:50:19 +00:00
|
|
|
|
2020-11-23 08:53:14 +00:00
|
|
|
subs->sync_endpoint->is_implicit_feedback = fmt->implicit_fb;
|
2020-05-10 18:29:11 +00:00
|
|
|
|
2013-08-03 08:50:19 +00:00
|
|
|
subs->data_endpoint->sync_master = subs->sync_endpoint;
|
|
|
|
|
2020-11-23 08:53:08 +00:00
|
|
|
if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
|
|
|
|
subs->data_endpoint->altsetting != subs->sync_endpoint->altsetting) {
|
|
|
|
err = usb_set_interface(subs->dev,
|
|
|
|
subs->sync_endpoint->iface,
|
|
|
|
subs->sync_endpoint->altsetting);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
dev_dbg(&dev->dev, "setting usb interface %d:%d\n",
|
|
|
|
subs->sync_endpoint->iface,
|
|
|
|
subs->sync_endpoint->altsetting);
|
|
|
|
snd_usb_set_interface_quirk(dev);
|
|
|
|
}
|
|
|
|
|
2013-08-03 08:50:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* find a matching format and set up the interface
|
|
|
|
*/
|
|
|
|
static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
|
|
|
|
{
|
|
|
|
struct usb_device *dev = subs->dev;
|
|
|
|
struct usb_host_interface *alts;
|
|
|
|
struct usb_interface *iface;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
iface = usb_ifnum_to_if(dev, fmt->iface);
|
|
|
|
if (WARN_ON(!iface))
|
|
|
|
return -EINVAL;
|
2018-05-02 07:36:28 +00:00
|
|
|
alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
|
2019-12-20 09:31:34 +00:00
|
|
|
if (WARN_ON(!alts))
|
2013-08-03 08:50:17 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-12-18 13:26:50 +00:00
|
|
|
if (fmt == subs->cur_audiofmt && !subs->need_setup_fmt)
|
2013-08-03 08:50:17 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* close the old interface */
|
2019-12-18 13:26:50 +00:00
|
|
|
if (subs->interface >= 0 && (subs->interface != fmt->iface || subs->need_setup_fmt)) {
|
2018-05-02 08:04:27 +00:00
|
|
|
if (!subs->stream->chip->keep_iface) {
|
|
|
|
err = usb_set_interface(subs->dev, subs->interface, 0);
|
|
|
|
if (err < 0) {
|
|
|
|
dev_err(&dev->dev,
|
|
|
|
"%d:%d: return to setting 0 failed (%d)\n",
|
|
|
|
fmt->iface, fmt->altsetting, err);
|
|
|
|
return -EIO;
|
|
|
|
}
|
2013-08-03 08:50:17 +00:00
|
|
|
}
|
|
|
|
subs->interface = -1;
|
|
|
|
subs->altset_idx = 0;
|
|
|
|
}
|
|
|
|
|
2019-12-18 13:26:50 +00:00
|
|
|
if (subs->need_setup_fmt)
|
|
|
|
subs->need_setup_fmt = false;
|
|
|
|
|
2013-08-03 08:50:17 +00:00
|
|
|
/* set interface */
|
2018-05-02 07:36:28 +00:00
|
|
|
if (iface->cur_altsetting != alts) {
|
2014-11-28 16:32:54 +00:00
|
|
|
err = snd_usb_select_mode_quirk(subs, fmt);
|
|
|
|
if (err < 0)
|
|
|
|
return -EIO;
|
|
|
|
|
2013-08-03 08:50:17 +00:00
|
|
|
err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
|
|
|
|
if (err < 0) {
|
2014-02-26 12:02:17 +00:00
|
|
|
dev_err(&dev->dev,
|
|
|
|
"%d:%d: usb_set_interface failed (%d)\n",
|
|
|
|
fmt->iface, fmt->altsetting, err);
|
2013-08-03 08:50:17 +00:00
|
|
|
return -EIO;
|
|
|
|
}
|
2014-02-26 12:02:17 +00:00
|
|
|
dev_dbg(&dev->dev, "setting usb interface %d:%d\n",
|
|
|
|
fmt->iface, fmt->altsetting);
|
2013-08-03 08:50:17 +00:00
|
|
|
snd_usb_set_interface_quirk(dev);
|
|
|
|
}
|
|
|
|
|
2018-05-02 07:36:28 +00:00
|
|
|
subs->interface = fmt->iface;
|
|
|
|
subs->altset_idx = fmt->altset_idx;
|
2013-08-03 08:50:17 +00:00
|
|
|
subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
|
|
|
|
alts, fmt->endpoint, subs->direction,
|
|
|
|
SND_USB_ENDPOINT_TYPE_DATA);
|
|
|
|
|
|
|
|
if (!subs->data_endpoint)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2020-11-23 08:53:14 +00:00
|
|
|
err = set_sync_endpoint(subs, fmt);
|
2013-08-03 08:50:17 +00:00
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
2013-08-03 08:50:16 +00:00
|
|
|
err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
|
|
|
|
if (err < 0)
|
2010-03-04 18:46:13 +00:00
|
|
|
return err;
|
|
|
|
|
|
|
|
subs->cur_audiofmt = fmt;
|
|
|
|
|
|
|
|
snd_usb_set_format_quirk(subs, fmt);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
ALSA: usb-audio: sync ep init fix for audioformat mismatch
Commit 947d299686aa9cc8aecf749d54e8475c6e498956 , "ALSA: snd-usb:
properly initialize the sync endpoint", while correcting the
initialization of the sync endpoint when opening just the data
endpoint, prevents devices that has a sync endpoint, with a channel
number different than that of the data endpoint, from functioning.
Due to a different channel and period bytes count, attempting to
initialize the sync endpoint will fail at the usb host driver.
For example, when using xhci:
cannot submit urb 0, error -90: internal error
With this patch, if a sync endpoint has multiple audioformats, a
matching audioformat is preferred. An audioformat must be found
with at least one channel and support the requested sample rate
and PCM format, otherwise the stream will not be opened.
If the number of channels differ between the selected audioformat
and the requested format, adjust the period bytes count accordingly.
It is safe to perform the calculation on the basis of the channel
count, since the requested PCM audio format and the rate must be
supported by the selected audioformat.
Cc: Jeffrey Barish <jeff_barish@earthlink.net>
Cc: Daniel Mack <zonque@gmail.com>
Signed-off-by: Eldad Zack <eldad@fogrefinery.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2012-12-03 19:30:09 +00:00
|
|
|
/*
|
|
|
|
* Return the score of matching two audioformats.
|
|
|
|
* Veto the audioformat if:
|
|
|
|
* - It has no channels for some reason.
|
|
|
|
* - Requested PCM format is not supported.
|
|
|
|
* - Requested sample rate is not supported.
|
|
|
|
*/
|
2014-02-26 12:02:17 +00:00
|
|
|
static int match_endpoint_audioformats(struct snd_usb_substream *subs,
|
|
|
|
struct audioformat *fp,
|
|
|
|
struct audioformat *match, int rate,
|
|
|
|
snd_pcm_format_t pcm_format)
|
ALSA: usb-audio: sync ep init fix for audioformat mismatch
Commit 947d299686aa9cc8aecf749d54e8475c6e498956 , "ALSA: snd-usb:
properly initialize the sync endpoint", while correcting the
initialization of the sync endpoint when opening just the data
endpoint, prevents devices that has a sync endpoint, with a channel
number different than that of the data endpoint, from functioning.
Due to a different channel and period bytes count, attempting to
initialize the sync endpoint will fail at the usb host driver.
For example, when using xhci:
cannot submit urb 0, error -90: internal error
With this patch, if a sync endpoint has multiple audioformats, a
matching audioformat is preferred. An audioformat must be found
with at least one channel and support the requested sample rate
and PCM format, otherwise the stream will not be opened.
If the number of channels differ between the selected audioformat
and the requested format, adjust the period bytes count accordingly.
It is safe to perform the calculation on the basis of the channel
count, since the requested PCM audio format and the rate must be
supported by the selected audioformat.
Cc: Jeffrey Barish <jeff_barish@earthlink.net>
Cc: Daniel Mack <zonque@gmail.com>
Signed-off-by: Eldad Zack <eldad@fogrefinery.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2012-12-03 19:30:09 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int score = 0;
|
|
|
|
|
|
|
|
if (fp->channels < 1) {
|
2014-02-26 12:02:17 +00:00
|
|
|
dev_dbg(&subs->dev->dev,
|
|
|
|
"%s: (fmt @%p) no channels\n", __func__, fp);
|
ALSA: usb-audio: sync ep init fix for audioformat mismatch
Commit 947d299686aa9cc8aecf749d54e8475c6e498956 , "ALSA: snd-usb:
properly initialize the sync endpoint", while correcting the
initialization of the sync endpoint when opening just the data
endpoint, prevents devices that has a sync endpoint, with a channel
number different than that of the data endpoint, from functioning.
Due to a different channel and period bytes count, attempting to
initialize the sync endpoint will fail at the usb host driver.
For example, when using xhci:
cannot submit urb 0, error -90: internal error
With this patch, if a sync endpoint has multiple audioformats, a
matching audioformat is preferred. An audioformat must be found
with at least one channel and support the requested sample rate
and PCM format, otherwise the stream will not be opened.
If the number of channels differ between the selected audioformat
and the requested format, adjust the period bytes count accordingly.
It is safe to perform the calculation on the basis of the channel
count, since the requested PCM audio format and the rate must be
supported by the selected audioformat.
Cc: Jeffrey Barish <jeff_barish@earthlink.net>
Cc: Daniel Mack <zonque@gmail.com>
Signed-off-by: Eldad Zack <eldad@fogrefinery.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2012-12-03 19:30:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-04-22 23:00:41 +00:00
|
|
|
if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
|
2014-02-26 12:02:17 +00:00
|
|
|
dev_dbg(&subs->dev->dev,
|
|
|
|
"%s: (fmt @%p) no match for format %d\n", __func__,
|
ALSA: usb-audio: sync ep init fix for audioformat mismatch
Commit 947d299686aa9cc8aecf749d54e8475c6e498956 , "ALSA: snd-usb:
properly initialize the sync endpoint", while correcting the
initialization of the sync endpoint when opening just the data
endpoint, prevents devices that has a sync endpoint, with a channel
number different than that of the data endpoint, from functioning.
Due to a different channel and period bytes count, attempting to
initialize the sync endpoint will fail at the usb host driver.
For example, when using xhci:
cannot submit urb 0, error -90: internal error
With this patch, if a sync endpoint has multiple audioformats, a
matching audioformat is preferred. An audioformat must be found
with at least one channel and support the requested sample rate
and PCM format, otherwise the stream will not be opened.
If the number of channels differ between the selected audioformat
and the requested format, adjust the period bytes count accordingly.
It is safe to perform the calculation on the basis of the channel
count, since the requested PCM audio format and the rate must be
supported by the selected audioformat.
Cc: Jeffrey Barish <jeff_barish@earthlink.net>
Cc: Daniel Mack <zonque@gmail.com>
Signed-off-by: Eldad Zack <eldad@fogrefinery.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2012-12-03 19:30:09 +00:00
|
|
|
fp, pcm_format);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < fp->nr_rates; i++) {
|
|
|
|
if (fp->rate_table[i] == rate) {
|
|
|
|
score++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!score) {
|
2014-02-26 12:02:17 +00:00
|
|
|
dev_dbg(&subs->dev->dev,
|
|
|
|
"%s: (fmt @%p) no match for rate %d\n", __func__,
|
ALSA: usb-audio: sync ep init fix for audioformat mismatch
Commit 947d299686aa9cc8aecf749d54e8475c6e498956 , "ALSA: snd-usb:
properly initialize the sync endpoint", while correcting the
initialization of the sync endpoint when opening just the data
endpoint, prevents devices that has a sync endpoint, with a channel
number different than that of the data endpoint, from functioning.
Due to a different channel and period bytes count, attempting to
initialize the sync endpoint will fail at the usb host driver.
For example, when using xhci:
cannot submit urb 0, error -90: internal error
With this patch, if a sync endpoint has multiple audioformats, a
matching audioformat is preferred. An audioformat must be found
with at least one channel and support the requested sample rate
and PCM format, otherwise the stream will not be opened.
If the number of channels differ between the selected audioformat
and the requested format, adjust the period bytes count accordingly.
It is safe to perform the calculation on the basis of the channel
count, since the requested PCM audio format and the rate must be
supported by the selected audioformat.
Cc: Jeffrey Barish <jeff_barish@earthlink.net>
Cc: Daniel Mack <zonque@gmail.com>
Signed-off-by: Eldad Zack <eldad@fogrefinery.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2012-12-03 19:30:09 +00:00
|
|
|
fp, rate);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fp->channels == match->channels)
|
|
|
|
score++;
|
|
|
|
|
2014-02-26 12:02:17 +00:00
|
|
|
dev_dbg(&subs->dev->dev,
|
|
|
|
"%s: (fmt @%p) score %d\n", __func__, fp, score);
|
ALSA: usb-audio: sync ep init fix for audioformat mismatch
Commit 947d299686aa9cc8aecf749d54e8475c6e498956 , "ALSA: snd-usb:
properly initialize the sync endpoint", while correcting the
initialization of the sync endpoint when opening just the data
endpoint, prevents devices that has a sync endpoint, with a channel
number different than that of the data endpoint, from functioning.
Due to a different channel and period bytes count, attempting to
initialize the sync endpoint will fail at the usb host driver.
For example, when using xhci:
cannot submit urb 0, error -90: internal error
With this patch, if a sync endpoint has multiple audioformats, a
matching audioformat is preferred. An audioformat must be found
with at least one channel and support the requested sample rate
and PCM format, otherwise the stream will not be opened.
If the number of channels differ between the selected audioformat
and the requested format, adjust the period bytes count accordingly.
It is safe to perform the calculation on the basis of the channel
count, since the requested PCM audio format and the rate must be
supported by the selected audioformat.
Cc: Jeffrey Barish <jeff_barish@earthlink.net>
Cc: Daniel Mack <zonque@gmail.com>
Signed-off-by: Eldad Zack <eldad@fogrefinery.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2012-12-03 19:30:09 +00:00
|
|
|
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Configure the sync ep using the rate and pcm format of the data ep.
|
|
|
|
*/
|
|
|
|
static int configure_sync_endpoint(struct snd_usb_substream *subs)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct audioformat *fp;
|
|
|
|
struct audioformat *sync_fp = NULL;
|
|
|
|
int cur_score = 0;
|
|
|
|
int sync_period_bytes = subs->period_bytes;
|
|
|
|
struct snd_usb_substream *sync_subs =
|
|
|
|
&subs->stream->substream[subs->direction ^ 1];
|
|
|
|
|
2013-01-10 13:06:38 +00:00
|
|
|
if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
|
|
|
|
!subs->stream)
|
|
|
|
return snd_usb_endpoint_set_params(subs->sync_endpoint,
|
|
|
|
subs->pcm_format,
|
|
|
|
subs->channels,
|
|
|
|
subs->period_bytes,
|
ALSA: improve buffer size computations for USB PCM audio
This patch changes the way URBs are allocated and their sizes are
determined for PCM playback in the snd-usb-audio driver. Currently
the driver allocates too few URBs for endpoints that don't use
implicit sync, making underruns more likely to occur. This may be a
holdover from before I/O delays could be measured accurately; in any
case, it is no longer necessary.
The patch allocates as many URBs as possible, subject to four
limitations:
The total number of URBs for the endpoint is not allowed to
exceed MAX_URBS (which the patch increases from 8 to 12).
The total number of packets per URB is not allowed to exceed
MAX_PACKS (or MAX_PACKS_HS for high-speed devices), which is
decreased from 20 to 6.
The total duration of queued data is not allowed to exceed
MAX_QUEUE, which is decreased from 24 ms to 18 ms.
The total number of ALSA frames in the output queue is not
allowed to exceed the ALSA buffer size.
The last requirement is the hardest to implement. Currently the
number of URBs needed to fill a buffer cannot be determined in
advance, because a buffer contains a fixed number of frames whereas
the number of frames in an URB varies to match shifts in the device's
clock rate. To solve this problem, the patch changes the logic for
deciding how many packets an URB should contain. Rather than using as
many as possible without exceeding an ALSA period boundary, now the
driver uses only as many packets as needed to transfer a predetermined
number of frames. As a result, unless the device's clock has an
exceedingly variable rate, the number of URBs making up each period
(and hence each buffer) will remain constant.
The overall effect of the patch is that playback works better in
low-latency settings. The user can still specify values for
frames/period and periods/buffer that exceed the capabilities of the
hardware, of course. But for values that are within those
capabilities, the performance will be improved. For example, testing
shows that a high-speed device can handle 32 frames/period and 3
periods/buffer at 48 KHz, whereas the current driver starts to get
glitchy at 64 frames/period and 2 periods/buffer.
A side effect of these changes is that the "nrpacks" module parameter
is no longer used. The patch removes it.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
CC: Clemens Ladisch <clemens@ladisch.de>
Tested-by: Daniel Mack <zonque@gmail.com>
Tested-by: Eldad Zack <eldad@fogrefinery.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2013-09-24 19:51:58 +00:00
|
|
|
0, 0,
|
2013-01-10 13:06:38 +00:00
|
|
|
subs->cur_rate,
|
|
|
|
subs->cur_audiofmt,
|
|
|
|
NULL);
|
|
|
|
|
ALSA: usb-audio: sync ep init fix for audioformat mismatch
Commit 947d299686aa9cc8aecf749d54e8475c6e498956 , "ALSA: snd-usb:
properly initialize the sync endpoint", while correcting the
initialization of the sync endpoint when opening just the data
endpoint, prevents devices that has a sync endpoint, with a channel
number different than that of the data endpoint, from functioning.
Due to a different channel and period bytes count, attempting to
initialize the sync endpoint will fail at the usb host driver.
For example, when using xhci:
cannot submit urb 0, error -90: internal error
With this patch, if a sync endpoint has multiple audioformats, a
matching audioformat is preferred. An audioformat must be found
with at least one channel and support the requested sample rate
and PCM format, otherwise the stream will not be opened.
If the number of channels differ between the selected audioformat
and the requested format, adjust the period bytes count accordingly.
It is safe to perform the calculation on the basis of the channel
count, since the requested PCM audio format and the rate must be
supported by the selected audioformat.
Cc: Jeffrey Barish <jeff_barish@earthlink.net>
Cc: Daniel Mack <zonque@gmail.com>
Signed-off-by: Eldad Zack <eldad@fogrefinery.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2012-12-03 19:30:09 +00:00
|
|
|
/* Try to find the best matching audioformat. */
|
|
|
|
list_for_each_entry(fp, &sync_subs->fmt_list, list) {
|
2014-02-26 12:02:17 +00:00
|
|
|
int score = match_endpoint_audioformats(subs,
|
|
|
|
fp, subs->cur_audiofmt,
|
ALSA: usb-audio: sync ep init fix for audioformat mismatch
Commit 947d299686aa9cc8aecf749d54e8475c6e498956 , "ALSA: snd-usb:
properly initialize the sync endpoint", while correcting the
initialization of the sync endpoint when opening just the data
endpoint, prevents devices that has a sync endpoint, with a channel
number different than that of the data endpoint, from functioning.
Due to a different channel and period bytes count, attempting to
initialize the sync endpoint will fail at the usb host driver.
For example, when using xhci:
cannot submit urb 0, error -90: internal error
With this patch, if a sync endpoint has multiple audioformats, a
matching audioformat is preferred. An audioformat must be found
with at least one channel and support the requested sample rate
and PCM format, otherwise the stream will not be opened.
If the number of channels differ between the selected audioformat
and the requested format, adjust the period bytes count accordingly.
It is safe to perform the calculation on the basis of the channel
count, since the requested PCM audio format and the rate must be
supported by the selected audioformat.
Cc: Jeffrey Barish <jeff_barish@earthlink.net>
Cc: Daniel Mack <zonque@gmail.com>
Signed-off-by: Eldad Zack <eldad@fogrefinery.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2012-12-03 19:30:09 +00:00
|
|
|
subs->cur_rate, subs->pcm_format);
|
|
|
|
|
|
|
|
if (score > cur_score) {
|
|
|
|
sync_fp = fp;
|
|
|
|
cur_score = score;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely(sync_fp == NULL)) {
|
2014-02-26 12:02:17 +00:00
|
|
|
dev_err(&subs->dev->dev,
|
|
|
|
"%s: no valid audioformat for sync ep %x found\n",
|
ALSA: usb-audio: sync ep init fix for audioformat mismatch
Commit 947d299686aa9cc8aecf749d54e8475c6e498956 , "ALSA: snd-usb:
properly initialize the sync endpoint", while correcting the
initialization of the sync endpoint when opening just the data
endpoint, prevents devices that has a sync endpoint, with a channel
number different than that of the data endpoint, from functioning.
Due to a different channel and period bytes count, attempting to
initialize the sync endpoint will fail at the usb host driver.
For example, when using xhci:
cannot submit urb 0, error -90: internal error
With this patch, if a sync endpoint has multiple audioformats, a
matching audioformat is preferred. An audioformat must be found
with at least one channel and support the requested sample rate
and PCM format, otherwise the stream will not be opened.
If the number of channels differ between the selected audioformat
and the requested format, adjust the period bytes count accordingly.
It is safe to perform the calculation on the basis of the channel
count, since the requested PCM audio format and the rate must be
supported by the selected audioformat.
Cc: Jeffrey Barish <jeff_barish@earthlink.net>
Cc: Daniel Mack <zonque@gmail.com>
Signed-off-by: Eldad Zack <eldad@fogrefinery.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2012-12-03 19:30:09 +00:00
|
|
|
__func__, sync_subs->ep_num);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recalculate the period bytes if channel number differ between
|
|
|
|
* data and sync ep audioformat.
|
|
|
|
*/
|
|
|
|
if (sync_fp->channels != subs->channels) {
|
|
|
|
sync_period_bytes = (subs->period_bytes / subs->channels) *
|
|
|
|
sync_fp->channels;
|
2014-02-26 12:02:17 +00:00
|
|
|
dev_dbg(&subs->dev->dev,
|
|
|
|
"%s: adjusted sync ep period bytes (%d -> %d)\n",
|
ALSA: usb-audio: sync ep init fix for audioformat mismatch
Commit 947d299686aa9cc8aecf749d54e8475c6e498956 , "ALSA: snd-usb:
properly initialize the sync endpoint", while correcting the
initialization of the sync endpoint when opening just the data
endpoint, prevents devices that has a sync endpoint, with a channel
number different than that of the data endpoint, from functioning.
Due to a different channel and period bytes count, attempting to
initialize the sync endpoint will fail at the usb host driver.
For example, when using xhci:
cannot submit urb 0, error -90: internal error
With this patch, if a sync endpoint has multiple audioformats, a
matching audioformat is preferred. An audioformat must be found
with at least one channel and support the requested sample rate
and PCM format, otherwise the stream will not be opened.
If the number of channels differ between the selected audioformat
and the requested format, adjust the period bytes count accordingly.
It is safe to perform the calculation on the basis of the channel
count, since the requested PCM audio format and the rate must be
supported by the selected audioformat.
Cc: Jeffrey Barish <jeff_barish@earthlink.net>
Cc: Daniel Mack <zonque@gmail.com>
Signed-off-by: Eldad Zack <eldad@fogrefinery.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2012-12-03 19:30:09 +00:00
|
|
|
__func__, subs->period_bytes, sync_period_bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
|
|
|
|
subs->pcm_format,
|
|
|
|
sync_fp->channels,
|
|
|
|
sync_period_bytes,
|
ALSA: improve buffer size computations for USB PCM audio
This patch changes the way URBs are allocated and their sizes are
determined for PCM playback in the snd-usb-audio driver. Currently
the driver allocates too few URBs for endpoints that don't use
implicit sync, making underruns more likely to occur. This may be a
holdover from before I/O delays could be measured accurately; in any
case, it is no longer necessary.
The patch allocates as many URBs as possible, subject to four
limitations:
The total number of URBs for the endpoint is not allowed to
exceed MAX_URBS (which the patch increases from 8 to 12).
The total number of packets per URB is not allowed to exceed
MAX_PACKS (or MAX_PACKS_HS for high-speed devices), which is
decreased from 20 to 6.
The total duration of queued data is not allowed to exceed
MAX_QUEUE, which is decreased from 24 ms to 18 ms.
The total number of ALSA frames in the output queue is not
allowed to exceed the ALSA buffer size.
The last requirement is the hardest to implement. Currently the
number of URBs needed to fill a buffer cannot be determined in
advance, because a buffer contains a fixed number of frames whereas
the number of frames in an URB varies to match shifts in the device's
clock rate. To solve this problem, the patch changes the logic for
deciding how many packets an URB should contain. Rather than using as
many as possible without exceeding an ALSA period boundary, now the
driver uses only as many packets as needed to transfer a predetermined
number of frames. As a result, unless the device's clock has an
exceedingly variable rate, the number of URBs making up each period
(and hence each buffer) will remain constant.
The overall effect of the patch is that playback works better in
low-latency settings. The user can still specify values for
frames/period and periods/buffer that exceed the capabilities of the
hardware, of course. But for values that are within those
capabilities, the performance will be improved. For example, testing
shows that a high-speed device can handle 32 frames/period and 3
periods/buffer at 48 KHz, whereas the current driver starts to get
glitchy at 64 frames/period and 2 periods/buffer.
A side effect of these changes is that the "nrpacks" module parameter
is no longer used. The patch removes it.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
CC: Clemens Ladisch <clemens@ladisch.de>
Tested-by: Daniel Mack <zonque@gmail.com>
Tested-by: Eldad Zack <eldad@fogrefinery.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2013-09-24 19:51:58 +00:00
|
|
|
0, 0,
|
ALSA: usb-audio: sync ep init fix for audioformat mismatch
Commit 947d299686aa9cc8aecf749d54e8475c6e498956 , "ALSA: snd-usb:
properly initialize the sync endpoint", while correcting the
initialization of the sync endpoint when opening just the data
endpoint, prevents devices that has a sync endpoint, with a channel
number different than that of the data endpoint, from functioning.
Due to a different channel and period bytes count, attempting to
initialize the sync endpoint will fail at the usb host driver.
For example, when using xhci:
cannot submit urb 0, error -90: internal error
With this patch, if a sync endpoint has multiple audioformats, a
matching audioformat is preferred. An audioformat must be found
with at least one channel and support the requested sample rate
and PCM format, otherwise the stream will not be opened.
If the number of channels differ between the selected audioformat
and the requested format, adjust the period bytes count accordingly.
It is safe to perform the calculation on the basis of the channel
count, since the requested PCM audio format and the rate must be
supported by the selected audioformat.
Cc: Jeffrey Barish <jeff_barish@earthlink.net>
Cc: Daniel Mack <zonque@gmail.com>
Signed-off-by: Eldad Zack <eldad@fogrefinery.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2012-12-03 19:30:09 +00:00
|
|
|
subs->cur_rate,
|
|
|
|
sync_fp,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-09-18 16:49:48 +00:00
|
|
|
/*
|
|
|
|
* configure endpoint params
|
|
|
|
*
|
|
|
|
* called during initial setup and upon resume
|
|
|
|
*/
|
|
|
|
static int configure_endpoint(struct snd_usb_substream *subs)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* format changed */
|
2019-12-10 06:34:54 +00:00
|
|
|
stop_endpoints(subs);
|
|
|
|
sync_pending_stops(subs);
|
2012-09-18 16:49:48 +00:00
|
|
|
ret = snd_usb_endpoint_set_params(subs->data_endpoint,
|
|
|
|
subs->pcm_format,
|
|
|
|
subs->channels,
|
|
|
|
subs->period_bytes,
|
ALSA: improve buffer size computations for USB PCM audio
This patch changes the way URBs are allocated and their sizes are
determined for PCM playback in the snd-usb-audio driver. Currently
the driver allocates too few URBs for endpoints that don't use
implicit sync, making underruns more likely to occur. This may be a
holdover from before I/O delays could be measured accurately; in any
case, it is no longer necessary.
The patch allocates as many URBs as possible, subject to four
limitations:
The total number of URBs for the endpoint is not allowed to
exceed MAX_URBS (which the patch increases from 8 to 12).
The total number of packets per URB is not allowed to exceed
MAX_PACKS (or MAX_PACKS_HS for high-speed devices), which is
decreased from 20 to 6.
The total duration of queued data is not allowed to exceed
MAX_QUEUE, which is decreased from 24 ms to 18 ms.
The total number of ALSA frames in the output queue is not
allowed to exceed the ALSA buffer size.
The last requirement is the hardest to implement. Currently the
number of URBs needed to fill a buffer cannot be determined in
advance, because a buffer contains a fixed number of frames whereas
the number of frames in an URB varies to match shifts in the device's
clock rate. To solve this problem, the patch changes the logic for
deciding how many packets an URB should contain. Rather than using as
many as possible without exceeding an ALSA period boundary, now the
driver uses only as many packets as needed to transfer a predetermined
number of frames. As a result, unless the device's clock has an
exceedingly variable rate, the number of URBs making up each period
(and hence each buffer) will remain constant.
The overall effect of the patch is that playback works better in
low-latency settings. The user can still specify values for
frames/period and periods/buffer that exceed the capabilities of the
hardware, of course. But for values that are within those
capabilities, the performance will be improved. For example, testing
shows that a high-speed device can handle 32 frames/period and 3
periods/buffer at 48 KHz, whereas the current driver starts to get
glitchy at 64 frames/period and 2 periods/buffer.
A side effect of these changes is that the "nrpacks" module parameter
is no longer used. The patch removes it.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
CC: Clemens Ladisch <clemens@ladisch.de>
Tested-by: Daniel Mack <zonque@gmail.com>
Tested-by: Eldad Zack <eldad@fogrefinery.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2013-09-24 19:51:58 +00:00
|
|
|
subs->period_frames,
|
|
|
|
subs->buffer_periods,
|
2012-09-18 16:49:48 +00:00
|
|
|
subs->cur_rate,
|
|
|
|
subs->cur_audiofmt,
|
|
|
|
subs->sync_endpoint);
|
|
|
|
if (ret < 0)
|
2012-10-12 13:12:55 +00:00
|
|
|
return ret;
|
2012-09-18 16:49:48 +00:00
|
|
|
|
|
|
|
if (subs->sync_endpoint)
|
ALSA: usb-audio: sync ep init fix for audioformat mismatch
Commit 947d299686aa9cc8aecf749d54e8475c6e498956 , "ALSA: snd-usb:
properly initialize the sync endpoint", while correcting the
initialization of the sync endpoint when opening just the data
endpoint, prevents devices that has a sync endpoint, with a channel
number different than that of the data endpoint, from functioning.
Due to a different channel and period bytes count, attempting to
initialize the sync endpoint will fail at the usb host driver.
For example, when using xhci:
cannot submit urb 0, error -90: internal error
With this patch, if a sync endpoint has multiple audioformats, a
matching audioformat is preferred. An audioformat must be found
with at least one channel and support the requested sample rate
and PCM format, otherwise the stream will not be opened.
If the number of channels differ between the selected audioformat
and the requested format, adjust the period bytes count accordingly.
It is safe to perform the calculation on the basis of the channel
count, since the requested PCM audio format and the rate must be
supported by the selected audioformat.
Cc: Jeffrey Barish <jeff_barish@earthlink.net>
Cc: Daniel Mack <zonque@gmail.com>
Signed-off-by: Eldad Zack <eldad@fogrefinery.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2012-12-03 19:30:09 +00:00
|
|
|
ret = configure_sync_endpoint(subs);
|
|
|
|
|
2012-09-18 16:49:48 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-07-31 12:28:44 +00:00
|
|
|
static int snd_usb_pcm_change_state(struct snd_usb_substream *subs, int state)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!subs->str_pd)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = snd_usb_power_domain_set(subs->stream->chip, subs->str_pd, state);
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&subs->dev->dev,
|
|
|
|
"Cannot change Power Domain ID: %d to state: %d. Err: %d\n",
|
|
|
|
subs->str_pd->pd_id, state, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int snd_usb_pcm_suspend(struct snd_usb_stream *as)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D2);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D2);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int snd_usb_pcm_resume(struct snd_usb_stream *as)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2018-07-31 12:28:45 +00:00
|
|
|
ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D1);
|
2018-07-31 12:28:44 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2018-07-31 12:28:45 +00:00
|
|
|
ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D1);
|
2018-07-31 12:28:44 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-03-04 18:46:13 +00:00
|
|
|
/*
|
|
|
|
* hw_params callback
|
|
|
|
*
|
|
|
|
* allocate a buffer and set the given audio format.
|
|
|
|
*
|
|
|
|
* so far we use a physically linear buffer although packetize transfer
|
|
|
|
* doesn't need a continuous area.
|
|
|
|
* if sg buffer is supported on the later version of alsa, we'll follow
|
|
|
|
* that.
|
|
|
|
*/
|
|
|
|
static int snd_usb_hw_params(struct snd_pcm_substream *substream,
|
|
|
|
struct snd_pcm_hw_params *hw_params)
|
|
|
|
{
|
|
|
|
struct snd_usb_substream *subs = substream->runtime->private_data;
|
|
|
|
struct audioformat *fmt;
|
2012-09-18 16:49:48 +00:00
|
|
|
int ret;
|
2010-03-04 18:46:13 +00:00
|
|
|
|
media: sound/usb: Use Media Controller API to share media resources
Media Device Allocator API to allows multiple drivers share a media device.
This API solves a very common use-case for media devices where one physical
device (an USB stick) provides both audio and video. When such media device
exposes a standard USB Audio class, a proprietary Video class, two or more
independent drivers will share a single physical USB bridge. In such cases,
it is necessary to coordinate access to the shared resource.
Using this API, drivers can allocate a media device with the shared struct
device as the key. Once the media device is allocated by a driver, other
drivers can get a reference to it. The media device is released when all
the references are released.
Change the ALSA driver to use the Media Controller API to share media
resources with DVB, and V4L2 drivers on a AU0828 media device.
The Media Controller specific initialization is done after sound card is
registered. ALSA creates Media interface and entity function graph nodes
for Control, Mixer, PCM Playback, and PCM Capture devices.
snd_usb_hw_params() will call Media Controller enable source handler
interface to request the media resource. If resource request is granted,
it will release it from snd_usb_hw_free(). If resource is busy, -EBUSY is
returned.
Media specific cleanup is done in usb_audio_disconnect().
Reviewed-by: Takashi Iwai <tiwai@suse.de>
Signed-off-by: Shuah Khan <shuah@kernel.org>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
2019-04-02 00:40:22 +00:00
|
|
|
ret = snd_media_start_pipeline(subs);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2012-09-18 16:49:48 +00:00
|
|
|
subs->pcm_format = params_format(hw_params);
|
|
|
|
subs->period_bytes = params_period_bytes(hw_params);
|
ALSA: improve buffer size computations for USB PCM audio
This patch changes the way URBs are allocated and their sizes are
determined for PCM playback in the snd-usb-audio driver. Currently
the driver allocates too few URBs for endpoints that don't use
implicit sync, making underruns more likely to occur. This may be a
holdover from before I/O delays could be measured accurately; in any
case, it is no longer necessary.
The patch allocates as many URBs as possible, subject to four
limitations:
The total number of URBs for the endpoint is not allowed to
exceed MAX_URBS (which the patch increases from 8 to 12).
The total number of packets per URB is not allowed to exceed
MAX_PACKS (or MAX_PACKS_HS for high-speed devices), which is
decreased from 20 to 6.
The total duration of queued data is not allowed to exceed
MAX_QUEUE, which is decreased from 24 ms to 18 ms.
The total number of ALSA frames in the output queue is not
allowed to exceed the ALSA buffer size.
The last requirement is the hardest to implement. Currently the
number of URBs needed to fill a buffer cannot be determined in
advance, because a buffer contains a fixed number of frames whereas
the number of frames in an URB varies to match shifts in the device's
clock rate. To solve this problem, the patch changes the logic for
deciding how many packets an URB should contain. Rather than using as
many as possible without exceeding an ALSA period boundary, now the
driver uses only as many packets as needed to transfer a predetermined
number of frames. As a result, unless the device's clock has an
exceedingly variable rate, the number of URBs making up each period
(and hence each buffer) will remain constant.
The overall effect of the patch is that playback works better in
low-latency settings. The user can still specify values for
frames/period and periods/buffer that exceed the capabilities of the
hardware, of course. But for values that are within those
capabilities, the performance will be improved. For example, testing
shows that a high-speed device can handle 32 frames/period and 3
periods/buffer at 48 KHz, whereas the current driver starts to get
glitchy at 64 frames/period and 2 periods/buffer.
A side effect of these changes is that the "nrpacks" module parameter
is no longer used. The patch removes it.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
CC: Clemens Ladisch <clemens@ladisch.de>
Tested-by: Daniel Mack <zonque@gmail.com>
Tested-by: Eldad Zack <eldad@fogrefinery.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2013-09-24 19:51:58 +00:00
|
|
|
subs->period_frames = params_period_size(hw_params);
|
|
|
|
subs->buffer_periods = params_periods(hw_params);
|
2012-09-18 16:49:48 +00:00
|
|
|
subs->channels = params_channels(hw_params);
|
|
|
|
subs->cur_rate = params_rate(hw_params);
|
|
|
|
|
|
|
|
fmt = find_format(subs);
|
2010-03-04 18:46:13 +00:00
|
|
|
if (!fmt) {
|
2014-02-26 12:02:17 +00:00
|
|
|
dev_dbg(&subs->dev->dev,
|
|
|
|
"cannot set format: format = %#x, rate = %d, channels = %d\n",
|
2012-09-18 16:49:48 +00:00
|
|
|
subs->pcm_format, subs->cur_rate, subs->channels);
|
media: sound/usb: Use Media Controller API to share media resources
Media Device Allocator API to allows multiple drivers share a media device.
This API solves a very common use-case for media devices where one physical
device (an USB stick) provides both audio and video. When such media device
exposes a standard USB Audio class, a proprietary Video class, two or more
independent drivers will share a single physical USB bridge. In such cases,
it is necessary to coordinate access to the shared resource.
Using this API, drivers can allocate a media device with the shared struct
device as the key. Once the media device is allocated by a driver, other
drivers can get a reference to it. The media device is released when all
the references are released.
Change the ALSA driver to use the Media Controller API to share media
resources with DVB, and V4L2 drivers on a AU0828 media device.
The Media Controller specific initialization is done after sound card is
registered. ALSA creates Media interface and entity function graph nodes
for Control, Mixer, PCM Playback, and PCM Capture devices.
snd_usb_hw_params() will call Media Controller enable source handler
interface to request the media resource. If resource request is granted,
it will release it from snd_usb_hw_free(). If resource is busy, -EBUSY is
returned.
Media specific cleanup is done in usb_audio_disconnect().
Reviewed-by: Takashi Iwai <tiwai@suse.de>
Signed-off-by: Shuah Khan <shuah@kernel.org>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
2019-04-02 00:40:22 +00:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto stop_pipeline;
|
2010-03-04 18:46:13 +00:00
|
|
|
}
|
|
|
|
|
ALSA: usb-audio: Avoid nested autoresume calls
After the recent fix of runtime PM for USB-audio driver, we got a
lockdep warning like:
=============================================
[ INFO: possible recursive locking detected ]
4.2.0-rc8+ #61 Not tainted
---------------------------------------------
pulseaudio/980 is trying to acquire lock:
(&chip->shutdown_rwsem){.+.+.+}, at: [<ffffffffa0355dac>] snd_usb_autoresume+0x1d/0x52 [snd_usb_audio]
but task is already holding lock:
(&chip->shutdown_rwsem){.+.+.+}, at: [<ffffffffa0355dac>] snd_usb_autoresume+0x1d/0x52 [snd_usb_audio]
This comes from snd_usb_autoresume() invoking down_read() and it's
used in a nested way. Although it's basically safe, per se (as these
are read locks), it's better to reduce such spurious warnings.
The read lock is needed to guarantee the execution of "shutdown"
(cleanup at disconnection) task after all concurrent tasks are
finished. This can be implemented in another better way.
Also, the current check of chip->in_pm isn't good enough for
protecting the racy execution of multiple auto-resumes.
This patch rewrites the logic of snd_usb_autoresume() & co; namely,
- The recursive call of autopm is avoided by the new refcount,
chip->active. The chip->in_pm flag is removed accordingly.
- Instead of rwsem, another refcount, chip->usage_count, is introduced
for tracking the period to delay the shutdown procedure. At
the last clear of this refcount, wake_up() to the shutdown waiter is
called.
- The shutdown flag is replaced with shutdown atomic count; this is
for reducing the lock.
- Two new helpers are introduced to simplify the management of these
refcounts; snd_usb_lock_shutdown() increases the usage_count, checks
the shutdown state, and does autoresume. snd_usb_unlock_shutdown()
does the opposite. Most of mixer and other codes just need this,
and simply returns an error if it receives an error from lock.
Fixes: 9003ebb13f61 ('ALSA: usb-audio: Fix runtime PM unbalance')
Reported-and-tested-by: Alexnader Kuleshov <kuleshovmail@gmail.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2015-08-25 14:09:00 +00:00
|
|
|
ret = snd_usb_lock_shutdown(subs->stream->chip);
|
|
|
|
if (ret < 0)
|
media: sound/usb: Use Media Controller API to share media resources
Media Device Allocator API to allows multiple drivers share a media device.
This API solves a very common use-case for media devices where one physical
device (an USB stick) provides both audio and video. When such media device
exposes a standard USB Audio class, a proprietary Video class, two or more
independent drivers will share a single physical USB bridge. In such cases,
it is necessary to coordinate access to the shared resource.
Using this API, drivers can allocate a media device with the shared struct
device as the key. Once the media device is allocated by a driver, other
drivers can get a reference to it. The media device is released when all
the references are released.
Change the ALSA driver to use the Media Controller API to share media
resources with DVB, and V4L2 drivers on a AU0828 media device.
The Media Controller specific initialization is done after sound card is
registered. ALSA creates Media interface and entity function graph nodes
for Control, Mixer, PCM Playback, and PCM Capture devices.
snd_usb_hw_params() will call Media Controller enable source handler
interface to request the media resource. If resource request is granted,
it will release it from snd_usb_hw_free(). If resource is busy, -EBUSY is
returned.
Media specific cleanup is done in usb_audio_disconnect().
Reviewed-by: Takashi Iwai <tiwai@suse.de>
Signed-off-by: Shuah Khan <shuah@kernel.org>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
2019-04-02 00:40:22 +00:00
|
|
|
goto stop_pipeline;
|
2018-07-31 12:28:45 +00:00
|
|
|
|
|
|
|
ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
|
|
|
|
if (ret < 0)
|
|
|
|
goto unlock;
|
|
|
|
|
ALSA: usb-audio: Avoid nested autoresume calls
After the recent fix of runtime PM for USB-audio driver, we got a
lockdep warning like:
=============================================
[ INFO: possible recursive locking detected ]
4.2.0-rc8+ #61 Not tainted
---------------------------------------------
pulseaudio/980 is trying to acquire lock:
(&chip->shutdown_rwsem){.+.+.+}, at: [<ffffffffa0355dac>] snd_usb_autoresume+0x1d/0x52 [snd_usb_audio]
but task is already holding lock:
(&chip->shutdown_rwsem){.+.+.+}, at: [<ffffffffa0355dac>] snd_usb_autoresume+0x1d/0x52 [snd_usb_audio]
This comes from snd_usb_autoresume() invoking down_read() and it's
used in a nested way. Although it's basically safe, per se (as these
are read locks), it's better to reduce such spurious warnings.
The read lock is needed to guarantee the execution of "shutdown"
(cleanup at disconnection) task after all concurrent tasks are
finished. This can be implemented in another better way.
Also, the current check of chip->in_pm isn't good enough for
protecting the racy execution of multiple auto-resumes.
This patch rewrites the logic of snd_usb_autoresume() & co; namely,
- The recursive call of autopm is avoided by the new refcount,
chip->active. The chip->in_pm flag is removed accordingly.
- Instead of rwsem, another refcount, chip->usage_count, is introduced
for tracking the period to delay the shutdown procedure. At
the last clear of this refcount, wake_up() to the shutdown waiter is
called.
- The shutdown flag is replaced with shutdown atomic count; this is
for reducing the lock.
- Two new helpers are introduced to simplify the management of these
refcounts; snd_usb_lock_shutdown() increases the usage_count, checks
the shutdown state, and does autoresume. snd_usb_unlock_shutdown()
does the opposite. Most of mixer and other codes just need this,
and simply returns an error if it receives an error from lock.
Fixes: 9003ebb13f61 ('ALSA: usb-audio: Fix runtime PM unbalance')
Reported-and-tested-by: Alexnader Kuleshov <kuleshovmail@gmail.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2015-08-25 14:09:00 +00:00
|
|
|
ret = set_format(subs, fmt);
|
2012-10-12 13:12:55 +00:00
|
|
|
if (ret < 0)
|
2018-07-31 12:28:45 +00:00
|
|
|
goto unlock;
|
2010-03-04 18:46:13 +00:00
|
|
|
|
2012-09-18 16:49:48 +00:00
|
|
|
subs->interface = fmt->iface;
|
|
|
|
subs->altset_idx = fmt->altset_idx;
|
2012-09-18 12:49:31 +00:00
|
|
|
subs->need_setup_ep = true;
|
2012-09-18 16:49:46 +00:00
|
|
|
|
2018-07-31 12:28:45 +00:00
|
|
|
unlock:
|
|
|
|
snd_usb_unlock_shutdown(subs->stream->chip);
|
media: sound/usb: Use Media Controller API to share media resources
Media Device Allocator API to allows multiple drivers share a media device.
This API solves a very common use-case for media devices where one physical
device (an USB stick) provides both audio and video. When such media device
exposes a standard USB Audio class, a proprietary Video class, two or more
independent drivers will share a single physical USB bridge. In such cases,
it is necessary to coordinate access to the shared resource.
Using this API, drivers can allocate a media device with the shared struct
device as the key. Once the media device is allocated by a driver, other
drivers can get a reference to it. The media device is released when all
the references are released.
Change the ALSA driver to use the Media Controller API to share media
resources with DVB, and V4L2 drivers on a AU0828 media device.
The Media Controller specific initialization is done after sound card is
registered. ALSA creates Media interface and entity function graph nodes
for Control, Mixer, PCM Playback, and PCM Capture devices.
snd_usb_hw_params() will call Media Controller enable source handler
interface to request the media resource. If resource request is granted,
it will release it from snd_usb_hw_free(). If resource is busy, -EBUSY is
returned.
Media specific cleanup is done in usb_audio_disconnect().
Reviewed-by: Takashi Iwai <tiwai@suse.de>
Signed-off-by: Shuah Khan <shuah@kernel.org>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
2019-04-02 00:40:22 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto stop_pipeline;
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
stop_pipeline:
|
|
|
|
snd_media_stop_pipeline(subs);
|
2018-07-31 12:28:45 +00:00
|
|
|
return ret;
|
2010-03-04 18:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* hw_free callback
|
|
|
|
*
|
|
|
|
* reset the audio format and release the buffer
|
|
|
|
*/
|
|
|
|
static int snd_usb_hw_free(struct snd_pcm_substream *substream)
|
|
|
|
{
|
|
|
|
struct snd_usb_substream *subs = substream->runtime->private_data;
|
|
|
|
|
media: sound/usb: Use Media Controller API to share media resources
Media Device Allocator API to allows multiple drivers share a media device.
This API solves a very common use-case for media devices where one physical
device (an USB stick) provides both audio and video. When such media device
exposes a standard USB Audio class, a proprietary Video class, two or more
independent drivers will share a single physical USB bridge. In such cases,
it is necessary to coordinate access to the shared resource.
Using this API, drivers can allocate a media device with the shared struct
device as the key. Once the media device is allocated by a driver, other
drivers can get a reference to it. The media device is released when all
the references are released.
Change the ALSA driver to use the Media Controller API to share media
resources with DVB, and V4L2 drivers on a AU0828 media device.
The Media Controller specific initialization is done after sound card is
registered. ALSA creates Media interface and entity function graph nodes
for Control, Mixer, PCM Playback, and PCM Capture devices.
snd_usb_hw_params() will call Media Controller enable source handler
interface to request the media resource. If resource request is granted,
it will release it from snd_usb_hw_free(). If resource is busy, -EBUSY is
returned.
Media specific cleanup is done in usb_audio_disconnect().
Reviewed-by: Takashi Iwai <tiwai@suse.de>
Signed-off-by: Shuah Khan <shuah@kernel.org>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
2019-04-02 00:40:22 +00:00
|
|
|
snd_media_stop_pipeline(subs);
|
2010-03-04 18:46:13 +00:00
|
|
|
subs->cur_audiofmt = NULL;
|
|
|
|
subs->cur_rate = 0;
|
|
|
|
subs->period_bytes = 0;
|
ALSA: usb-audio: Avoid nested autoresume calls
After the recent fix of runtime PM for USB-audio driver, we got a
lockdep warning like:
=============================================
[ INFO: possible recursive locking detected ]
4.2.0-rc8+ #61 Not tainted
---------------------------------------------
pulseaudio/980 is trying to acquire lock:
(&chip->shutdown_rwsem){.+.+.+}, at: [<ffffffffa0355dac>] snd_usb_autoresume+0x1d/0x52 [snd_usb_audio]
but task is already holding lock:
(&chip->shutdown_rwsem){.+.+.+}, at: [<ffffffffa0355dac>] snd_usb_autoresume+0x1d/0x52 [snd_usb_audio]
This comes from snd_usb_autoresume() invoking down_read() and it's
used in a nested way. Although it's basically safe, per se (as these
are read locks), it's better to reduce such spurious warnings.
The read lock is needed to guarantee the execution of "shutdown"
(cleanup at disconnection) task after all concurrent tasks are
finished. This can be implemented in another better way.
Also, the current check of chip->in_pm isn't good enough for
protecting the racy execution of multiple auto-resumes.
This patch rewrites the logic of snd_usb_autoresume() & co; namely,
- The recursive call of autopm is avoided by the new refcount,
chip->active. The chip->in_pm flag is removed accordingly.
- Instead of rwsem, another refcount, chip->usage_count, is introduced
for tracking the period to delay the shutdown procedure. At
the last clear of this refcount, wake_up() to the shutdown waiter is
called.
- The shutdown flag is replaced with shutdown atomic count; this is
for reducing the lock.
- Two new helpers are introduced to simplify the management of these
refcounts; snd_usb_lock_shutdown() increases the usage_count, checks
the shutdown state, and does autoresume. snd_usb_unlock_shutdown()
does the opposite. Most of mixer and other codes just need this,
and simply returns an error if it receives an error from lock.
Fixes: 9003ebb13f61 ('ALSA: usb-audio: Fix runtime PM unbalance')
Reported-and-tested-by: Alexnader Kuleshov <kuleshovmail@gmail.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2015-08-25 14:09:00 +00:00
|
|
|
if (!snd_usb_lock_shutdown(subs->stream->chip)) {
|
2019-12-10 06:34:54 +00:00
|
|
|
stop_endpoints(subs);
|
|
|
|
sync_pending_stops(subs);
|
2013-10-06 20:31:07 +00:00
|
|
|
snd_usb_endpoint_deactivate(subs->sync_endpoint);
|
|
|
|
snd_usb_endpoint_deactivate(subs->data_endpoint);
|
2020-11-23 08:53:12 +00:00
|
|
|
if (subs->data_endpoint) {
|
|
|
|
subs->data_endpoint->sync_master = NULL;
|
|
|
|
subs->data_endpoint = NULL;
|
|
|
|
}
|
|
|
|
subs->sync_endpoint = NULL;
|
ALSA: usb-audio: Avoid nested autoresume calls
After the recent fix of runtime PM for USB-audio driver, we got a
lockdep warning like:
=============================================
[ INFO: possible recursive locking detected ]
4.2.0-rc8+ #61 Not tainted
---------------------------------------------
pulseaudio/980 is trying to acquire lock:
(&chip->shutdown_rwsem){.+.+.+}, at: [<ffffffffa0355dac>] snd_usb_autoresume+0x1d/0x52 [snd_usb_audio]
but task is already holding lock:
(&chip->shutdown_rwsem){.+.+.+}, at: [<ffffffffa0355dac>] snd_usb_autoresume+0x1d/0x52 [snd_usb_audio]
This comes from snd_usb_autoresume() invoking down_read() and it's
used in a nested way. Although it's basically safe, per se (as these
are read locks), it's better to reduce such spurious warnings.
The read lock is needed to guarantee the execution of "shutdown"
(cleanup at disconnection) task after all concurrent tasks are
finished. This can be implemented in another better way.
Also, the current check of chip->in_pm isn't good enough for
protecting the racy execution of multiple auto-resumes.
This patch rewrites the logic of snd_usb_autoresume() & co; namely,
- The recursive call of autopm is avoided by the new refcount,
chip->active. The chip->in_pm flag is removed accordingly.
- Instead of rwsem, another refcount, chip->usage_count, is introduced
for tracking the period to delay the shutdown procedure. At
the last clear of this refcount, wake_up() to the shutdown waiter is
called.
- The shutdown flag is replaced with shutdown atomic count; this is
for reducing the lock.
- Two new helpers are introduced to simplify the management of these
refcounts; snd_usb_lock_shutdown() increases the usage_count, checks
the shutdown state, and does autoresume. snd_usb_unlock_shutdown()
does the opposite. Most of mixer and other codes just need this,
and simply returns an error if it receives an error from lock.
Fixes: 9003ebb13f61 ('ALSA: usb-audio: Fix runtime PM unbalance')
Reported-and-tested-by: Alexnader Kuleshov <kuleshovmail@gmail.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2015-08-25 14:09:00 +00:00
|
|
|
snd_usb_unlock_shutdown(subs->stream->chip);
|
2012-10-12 13:12:55 +00:00
|
|
|
}
|
2018-05-27 11:01:17 +00:00
|
|
|
|
2019-12-09 09:49:42 +00:00
|
|
|
return 0;
|
2010-03-04 18:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* prepare callback
|
|
|
|
*
|
|
|
|
* only a few subtle things...
|
|
|
|
*/
|
|
|
|
static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
|
|
|
|
{
|
|
|
|
struct snd_pcm_runtime *runtime = substream->runtime;
|
|
|
|
struct snd_usb_substream *subs = runtime->private_data;
|
2012-09-18 16:49:48 +00:00
|
|
|
struct usb_host_interface *alts;
|
|
|
|
struct usb_interface *iface;
|
|
|
|
int ret;
|
2010-03-04 18:46:13 +00:00
|
|
|
|
|
|
|
if (! subs->cur_audiofmt) {
|
2014-02-26 12:02:17 +00:00
|
|
|
dev_err(&subs->dev->dev, "no format is specified!\n");
|
2010-03-04 18:46:13 +00:00
|
|
|
return -ENXIO;
|
|
|
|
}
|
|
|
|
|
ALSA: usb-audio: Avoid nested autoresume calls
After the recent fix of runtime PM for USB-audio driver, we got a
lockdep warning like:
=============================================
[ INFO: possible recursive locking detected ]
4.2.0-rc8+ #61 Not tainted
---------------------------------------------
pulseaudio/980 is trying to acquire lock:
(&chip->shutdown_rwsem){.+.+.+}, at: [<ffffffffa0355dac>] snd_usb_autoresume+0x1d/0x52 [snd_usb_audio]
but task is already holding lock:
(&chip->shutdown_rwsem){.+.+.+}, at: [<ffffffffa0355dac>] snd_usb_autoresume+0x1d/0x52 [snd_usb_audio]
This comes from snd_usb_autoresume() invoking down_read() and it's
used in a nested way. Although it's basically safe, per se (as these
are read locks), it's better to reduce such spurious warnings.
The read lock is needed to guarantee the execution of "shutdown"
(cleanup at disconnection) task after all concurrent tasks are
finished. This can be implemented in another better way.
Also, the current check of chip->in_pm isn't good enough for
protecting the racy execution of multiple auto-resumes.
This patch rewrites the logic of snd_usb_autoresume() & co; namely,
- The recursive call of autopm is avoided by the new refcount,
chip->active. The chip->in_pm flag is removed accordingly.
- Instead of rwsem, another refcount, chip->usage_count, is introduced
for tracking the period to delay the shutdown procedure. At
the last clear of this refcount, wake_up() to the shutdown waiter is
called.
- The shutdown flag is replaced with shutdown atomic count; this is
for reducing the lock.
- Two new helpers are introduced to simplify the management of these
refcounts; snd_usb_lock_shutdown() increases the usage_count, checks
the shutdown state, and does autoresume. snd_usb_unlock_shutdown()
does the opposite. Most of mixer and other codes just need this,
and simply returns an error if it receives an error from lock.
Fixes: 9003ebb13f61 ('ALSA: usb-audio: Fix runtime PM unbalance')
Reported-and-tested-by: Alexnader Kuleshov <kuleshovmail@gmail.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2015-08-25 14:09:00 +00:00
|
|
|
ret = snd_usb_lock_shutdown(subs->stream->chip);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-10-12 13:12:55 +00:00
|
|
|
if (snd_BUG_ON(!subs->data_endpoint)) {
|
|
|
|
ret = -EIO;
|
|
|
|
goto unlock;
|
|
|
|
}
|
2012-04-12 11:51:12 +00:00
|
|
|
|
2018-07-31 12:28:45 +00:00
|
|
|
ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
|
|
|
|
if (ret < 0)
|
|
|
|
goto unlock;
|
|
|
|
|
2012-09-18 16:49:48 +00:00
|
|
|
ret = set_format(subs, subs->cur_audiofmt);
|
|
|
|
if (ret < 0)
|
2012-10-12 13:12:55 +00:00
|
|
|
goto unlock;
|
2012-09-18 16:49:48 +00:00
|
|
|
|
2012-09-18 12:49:31 +00:00
|
|
|
if (subs->need_setup_ep) {
|
2016-12-06 05:46:15 +00:00
|
|
|
|
|
|
|
iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
|
|
|
|
alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
|
|
|
|
ret = snd_usb_init_sample_rate(subs->stream->chip,
|
|
|
|
subs->cur_audiofmt->iface,
|
|
|
|
alts,
|
|
|
|
subs->cur_audiofmt,
|
|
|
|
subs->cur_rate);
|
|
|
|
if (ret < 0)
|
|
|
|
goto unlock;
|
|
|
|
|
2012-09-18 12:49:31 +00:00
|
|
|
ret = configure_endpoint(subs);
|
|
|
|
if (ret < 0)
|
2012-10-12 13:12:55 +00:00
|
|
|
goto unlock;
|
2012-09-18 12:49:31 +00:00
|
|
|
subs->need_setup_ep = false;
|
|
|
|
}
|
2012-09-18 16:49:48 +00:00
|
|
|
|
2010-03-04 18:46:13 +00:00
|
|
|
/* some unit conversions in runtime */
|
2012-04-12 11:51:12 +00:00
|
|
|
subs->data_endpoint->maxframesize =
|
|
|
|
bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
|
|
|
|
subs->data_endpoint->curframesize =
|
|
|
|
bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
|
2010-03-04 18:46:13 +00:00
|
|
|
|
|
|
|
/* reset the pointer */
|
|
|
|
subs->hwptr_done = 0;
|
|
|
|
subs->transfer_done = 0;
|
2011-09-07 00:15:34 +00:00
|
|
|
subs->last_delay = 0;
|
|
|
|
subs->last_frame_number = 0;
|
2010-03-04 18:46:13 +00:00
|
|
|
runtime->delay = 0;
|
|
|
|
|
2012-04-12 11:51:12 +00:00
|
|
|
/* for playback, submit the URBs now; otherwise, the first hwptr_done
|
|
|
|
* updates for all URBs would happen at the same time when starting */
|
|
|
|
if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
|
2017-01-04 22:37:46 +00:00
|
|
|
ret = start_endpoints(subs);
|
2012-04-12 11:51:12 +00:00
|
|
|
|
2012-10-12 13:12:55 +00:00
|
|
|
unlock:
|
ALSA: usb-audio: Avoid nested autoresume calls
After the recent fix of runtime PM for USB-audio driver, we got a
lockdep warning like:
=============================================
[ INFO: possible recursive locking detected ]
4.2.0-rc8+ #61 Not tainted
---------------------------------------------
pulseaudio/980 is trying to acquire lock:
(&chip->shutdown_rwsem){.+.+.+}, at: [<ffffffffa0355dac>] snd_usb_autoresume+0x1d/0x52 [snd_usb_audio]
but task is already holding lock:
(&chip->shutdown_rwsem){.+.+.+}, at: [<ffffffffa0355dac>] snd_usb_autoresume+0x1d/0x52 [snd_usb_audio]
This comes from snd_usb_autoresume() invoking down_read() and it's
used in a nested way. Although it's basically safe, per se (as these
are read locks), it's better to reduce such spurious warnings.
The read lock is needed to guarantee the execution of "shutdown"
(cleanup at disconnection) task after all concurrent tasks are
finished. This can be implemented in another better way.
Also, the current check of chip->in_pm isn't good enough for
protecting the racy execution of multiple auto-resumes.
This patch rewrites the logic of snd_usb_autoresume() & co; namely,
- The recursive call of autopm is avoided by the new refcount,
chip->active. The chip->in_pm flag is removed accordingly.
- Instead of rwsem, another refcount, chip->usage_count, is introduced
for tracking the period to delay the shutdown procedure. At
the last clear of this refcount, wake_up() to the shutdown waiter is
called.
- The shutdown flag is replaced with shutdown atomic count; this is
for reducing the lock.
- Two new helpers are introduced to simplify the management of these
refcounts; snd_usb_lock_shutdown() increases the usage_count, checks
the shutdown state, and does autoresume. snd_usb_unlock_shutdown()
does the opposite. Most of mixer and other codes just need this,
and simply returns an error if it receives an error from lock.
Fixes: 9003ebb13f61 ('ALSA: usb-audio: Fix runtime PM unbalance')
Reported-and-tested-by: Alexnader Kuleshov <kuleshovmail@gmail.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2015-08-25 14:09:00 +00:00
|
|
|
snd_usb_unlock_shutdown(subs->stream->chip);
|
2012-10-12 13:12:55 +00:00
|
|
|
return ret;
|
2010-03-04 18:46:13 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 09:15:59 +00:00
|
|
|
static const struct snd_pcm_hardware snd_usb_hardware =
|
2010-03-04 18:46:13 +00:00
|
|
|
{
|
|
|
|
.info = SNDRV_PCM_INFO_MMAP |
|
|
|
|
SNDRV_PCM_INFO_MMAP_VALID |
|
|
|
|
SNDRV_PCM_INFO_BATCH |
|
|
|
|
SNDRV_PCM_INFO_INTERLEAVED |
|
|
|
|
SNDRV_PCM_INFO_BLOCK_TRANSFER |
|
|
|
|
SNDRV_PCM_INFO_PAUSE,
|
|
|
|
.buffer_bytes_max = 1024 * 1024,
|
|
|
|
.period_bytes_min = 64,
|
|
|
|
.period_bytes_max = 512 * 1024,
|
|
|
|
.periods_min = 2,
|
|
|
|
.periods_max = 1024,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int hw_check_valid_format(struct snd_usb_substream *subs,
|
|
|
|
struct snd_pcm_hw_params *params,
|
|
|
|
struct audioformat *fp)
|
|
|
|
{
|
|
|
|
struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
|
|
|
|
struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
|
|
|
|
struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
|
|
|
|
struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
|
2010-03-04 18:46:15 +00:00
|
|
|
struct snd_mask check_fmts;
|
2010-03-04 18:46:13 +00:00
|
|
|
unsigned int ptime;
|
|
|
|
|
|
|
|
/* check the format */
|
2010-03-04 18:46:15 +00:00
|
|
|
snd_mask_none(&check_fmts);
|
|
|
|
check_fmts.bits[0] = (u32)fp->formats;
|
|
|
|
check_fmts.bits[1] = (u32)(fp->formats >> 32);
|
|
|
|
snd_mask_intersect(&check_fmts, fmts);
|
|
|
|
if (snd_mask_empty(&check_fmts)) {
|
2010-03-04 18:46:13 +00:00
|
|
|
hwc_debug(" > check: no supported format %d\n", fp->format);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* check the channels */
|
|
|
|
if (fp->channels < ct->min || fp->channels > ct->max) {
|
|
|
|
hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* check the rate is within the range */
|
|
|
|
if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
|
|
|
|
hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
|
|
|
|
hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* check whether the period time is >= the data packet interval */
|
2012-10-12 13:12:55 +00:00
|
|
|
if (subs->speed != USB_SPEED_FULL) {
|
2010-03-04 18:46:13 +00:00
|
|
|
ptime = 125 * (1 << fp->datainterval);
|
|
|
|
if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
|
|
|
|
hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hw_rule_rate(struct snd_pcm_hw_params *params,
|
|
|
|
struct snd_pcm_hw_rule *rule)
|
|
|
|
{
|
|
|
|
struct snd_usb_substream *subs = rule->private;
|
2013-04-03 21:18:49 +00:00
|
|
|
struct audioformat *fp;
|
2010-03-04 18:46:13 +00:00
|
|
|
struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
|
2020-11-23 08:53:07 +00:00
|
|
|
unsigned int rmin, rmax, r;
|
2010-03-04 18:46:13 +00:00
|
|
|
int changed;
|
2020-11-23 08:53:07 +00:00
|
|
|
int i;
|
2010-03-04 18:46:13 +00:00
|
|
|
|
|
|
|
hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
|
2020-11-23 08:53:07 +00:00
|
|
|
rmin = UINT_MAX;
|
|
|
|
rmax = 0;
|
2013-04-03 21:18:49 +00:00
|
|
|
list_for_each_entry(fp, &subs->fmt_list, list) {
|
2010-03-04 18:46:13 +00:00
|
|
|
if (!hw_check_valid_format(subs, params, fp))
|
|
|
|
continue;
|
2020-11-23 08:53:07 +00:00
|
|
|
if (fp->rate_table && fp->nr_rates) {
|
|
|
|
for (i = 0; i < fp->nr_rates; i++) {
|
|
|
|
r = fp->rate_table[i];
|
|
|
|
if (!snd_interval_test(it, r))
|
|
|
|
continue;
|
|
|
|
rmin = min(rmin, r);
|
|
|
|
rmax = max(rmax, r);
|
|
|
|
}
|
2010-03-04 18:46:13 +00:00
|
|
|
} else {
|
2020-11-23 08:53:07 +00:00
|
|
|
rmin = min(rmin, fp->rate_min);
|
|
|
|
rmax = max(rmax, fp->rate_max);
|
2010-03-04 18:46:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 08:53:07 +00:00
|
|
|
if (rmin > rmax) {
|
2010-03-04 18:46:13 +00:00
|
|
|
hwc_debug(" --> get empty\n");
|
|
|
|
it->empty = 1;
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
changed = 0;
|
|
|
|
if (it->min < rmin) {
|
|
|
|
it->min = rmin;
|
|
|
|
it->openmin = 0;
|
|
|
|
changed = 1;
|
|
|
|
}
|
|
|
|
if (it->max > rmax) {
|
|
|
|
it->max = rmax;
|
|
|
|
it->openmax = 0;
|
|
|
|
changed = 1;
|
|
|
|
}
|
|
|
|
if (snd_interval_checkempty(it)) {
|
|
|
|
it->empty = 1;
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int hw_rule_channels(struct snd_pcm_hw_params *params,
|
|
|
|
struct snd_pcm_hw_rule *rule)
|
|
|
|
{
|
|
|
|
struct snd_usb_substream *subs = rule->private;
|
2013-04-03 21:18:49 +00:00
|
|
|
struct audioformat *fp;
|
2010-03-04 18:46:13 +00:00
|
|
|
struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
|
|
|
|
unsigned int rmin, rmax;
|
|
|
|
int changed;
|
|
|
|
|
|
|
|
hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
|
|
|
|
changed = 0;
|
|
|
|
rmin = rmax = 0;
|
2013-04-03 21:18:49 +00:00
|
|
|
list_for_each_entry(fp, &subs->fmt_list, list) {
|
2010-03-04 18:46:13 +00:00
|
|
|
if (!hw_check_valid_format(subs, params, fp))
|
|
|
|
continue;
|
|
|
|
if (changed++) {
|
|
|
|
if (rmin > fp->channels)
|
|
|
|
rmin = fp->channels;
|
|
|
|
if (rmax < fp->channels)
|
|
|
|
rmax = fp->channels;
|
|
|
|
} else {
|
|
|
|
rmin = fp->channels;
|
|
|
|
rmax = fp->channels;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!changed) {
|
|
|
|
hwc_debug(" --> get empty\n");
|
|
|
|
it->empty = 1;
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
changed = 0;
|
|
|
|
if (it->min < rmin) {
|
|
|
|
it->min = rmin;
|
|
|
|
it->openmin = 0;
|
|
|
|
changed = 1;
|
|
|
|
}
|
|
|
|
if (it->max > rmax) {
|
|
|
|
it->max = rmax;
|
|
|
|
it->openmax = 0;
|
|
|
|
changed = 1;
|
|
|
|
}
|
|
|
|
if (snd_interval_checkempty(it)) {
|
|
|
|
it->empty = 1;
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hw_rule_format(struct snd_pcm_hw_params *params,
|
|
|
|
struct snd_pcm_hw_rule *rule)
|
|
|
|
{
|
|
|
|
struct snd_usb_substream *subs = rule->private;
|
2013-04-03 21:18:49 +00:00
|
|
|
struct audioformat *fp;
|
2010-03-04 18:46:13 +00:00
|
|
|
struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
|
|
|
|
u64 fbits;
|
|
|
|
u32 oldbits[2];
|
|
|
|
int changed;
|
|
|
|
|
|
|
|
hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
|
|
|
|
fbits = 0;
|
2013-04-03 21:18:49 +00:00
|
|
|
list_for_each_entry(fp, &subs->fmt_list, list) {
|
2010-03-04 18:46:13 +00:00
|
|
|
if (!hw_check_valid_format(subs, params, fp))
|
|
|
|
continue;
|
2010-03-04 18:46:15 +00:00
|
|
|
fbits |= fp->formats;
|
2010-03-04 18:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
oldbits[0] = fmt->bits[0];
|
|
|
|
oldbits[1] = fmt->bits[1];
|
|
|
|
fmt->bits[0] &= (u32)fbits;
|
|
|
|
fmt->bits[1] &= (u32)(fbits >> 32);
|
|
|
|
if (!fmt->bits[0] && !fmt->bits[1]) {
|
|
|
|
hwc_debug(" --> get empty\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
|
|
|
|
hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hw_rule_period_time(struct snd_pcm_hw_params *params,
|
|
|
|
struct snd_pcm_hw_rule *rule)
|
|
|
|
{
|
|
|
|
struct snd_usb_substream *subs = rule->private;
|
|
|
|
struct audioformat *fp;
|
|
|
|
struct snd_interval *it;
|
|
|
|
unsigned char min_datainterval;
|
|
|
|
unsigned int pmin;
|
|
|
|
int changed;
|
|
|
|
|
|
|
|
it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
|
|
|
|
hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
|
|
|
|
min_datainterval = 0xff;
|
|
|
|
list_for_each_entry(fp, &subs->fmt_list, list) {
|
|
|
|
if (!hw_check_valid_format(subs, params, fp))
|
|
|
|
continue;
|
|
|
|
min_datainterval = min(min_datainterval, fp->datainterval);
|
|
|
|
}
|
|
|
|
if (min_datainterval == 0xff) {
|
2010-07-12 15:15:44 +00:00
|
|
|
hwc_debug(" --> get empty\n");
|
2010-03-04 18:46:13 +00:00
|
|
|
it->empty = 1;
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
pmin = 125 * (1 << min_datainterval);
|
|
|
|
changed = 0;
|
|
|
|
if (it->min < pmin) {
|
|
|
|
it->min = pmin;
|
|
|
|
it->openmin = 0;
|
|
|
|
changed = 1;
|
|
|
|
}
|
|
|
|
if (snd_interval_checkempty(it)) {
|
|
|
|
it->empty = 1;
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set up the runtime hardware information.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
|
|
|
|
{
|
2013-04-03 21:18:49 +00:00
|
|
|
struct audioformat *fp;
|
2010-03-04 18:46:13 +00:00
|
|
|
unsigned int pt, ptmin;
|
|
|
|
int param_period_time_if_needed;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
runtime->hw.formats = subs->formats;
|
|
|
|
|
|
|
|
runtime->hw.rate_min = 0x7fffffff;
|
|
|
|
runtime->hw.rate_max = 0;
|
|
|
|
runtime->hw.channels_min = 256;
|
|
|
|
runtime->hw.channels_max = 0;
|
|
|
|
runtime->hw.rates = 0;
|
|
|
|
ptmin = UINT_MAX;
|
|
|
|
/* check min/max rates and channels */
|
2013-04-03 21:18:49 +00:00
|
|
|
list_for_each_entry(fp, &subs->fmt_list, list) {
|
2010-03-04 18:46:13 +00:00
|
|
|
runtime->hw.rates |= fp->rates;
|
|
|
|
if (runtime->hw.rate_min > fp->rate_min)
|
|
|
|
runtime->hw.rate_min = fp->rate_min;
|
|
|
|
if (runtime->hw.rate_max < fp->rate_max)
|
|
|
|
runtime->hw.rate_max = fp->rate_max;
|
|
|
|
if (runtime->hw.channels_min > fp->channels)
|
|
|
|
runtime->hw.channels_min = fp->channels;
|
|
|
|
if (runtime->hw.channels_max < fp->channels)
|
|
|
|
runtime->hw.channels_max = fp->channels;
|
|
|
|
if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
|
|
|
|
/* FIXME: there might be more than one audio formats... */
|
|
|
|
runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
|
|
|
|
fp->frame_size;
|
|
|
|
}
|
|
|
|
pt = 125 * (1 << fp->datainterval);
|
|
|
|
ptmin = min(ptmin, pt);
|
|
|
|
}
|
|
|
|
|
|
|
|
param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
|
2012-10-12 13:12:55 +00:00
|
|
|
if (subs->speed == USB_SPEED_FULL)
|
2010-03-04 18:46:13 +00:00
|
|
|
/* full speed devices have fixed data packet interval */
|
|
|
|
ptmin = 1000;
|
|
|
|
if (ptmin == 1000)
|
|
|
|
/* if period time doesn't go below 1 ms, no rules needed */
|
|
|
|
param_period_time_if_needed = -1;
|
2018-05-27 13:09:15 +00:00
|
|
|
|
|
|
|
err = snd_pcm_hw_constraint_minmax(runtime,
|
|
|
|
SNDRV_PCM_HW_PARAM_PERIOD_TIME,
|
|
|
|
ptmin, UINT_MAX);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
|
|
|
|
hw_rule_rate, subs,
|
|
|
|
SNDRV_PCM_HW_PARAM_FORMAT,
|
|
|
|
SNDRV_PCM_HW_PARAM_CHANNELS,
|
|
|
|
param_period_time_if_needed,
|
|
|
|
-1);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
|
|
|
|
hw_rule_channels, subs,
|
|
|
|
SNDRV_PCM_HW_PARAM_FORMAT,
|
|
|
|
SNDRV_PCM_HW_PARAM_RATE,
|
|
|
|
param_period_time_if_needed,
|
|
|
|
-1);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
|
|
|
|
hw_rule_format, subs,
|
|
|
|
SNDRV_PCM_HW_PARAM_RATE,
|
|
|
|
SNDRV_PCM_HW_PARAM_CHANNELS,
|
|
|
|
param_period_time_if_needed,
|
|
|
|
-1);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2010-03-04 18:46:13 +00:00
|
|
|
if (param_period_time_if_needed >= 0) {
|
|
|
|
err = snd_pcm_hw_rule_add(runtime, 0,
|
|
|
|
SNDRV_PCM_HW_PARAM_PERIOD_TIME,
|
|
|
|
hw_rule_period_time, subs,
|
|
|
|
SNDRV_PCM_HW_PARAM_FORMAT,
|
|
|
|
SNDRV_PCM_HW_PARAM_CHANNELS,
|
|
|
|
SNDRV_PCM_HW_PARAM_RATE,
|
|
|
|
-1);
|
|
|
|
if (err < 0)
|
2018-05-27 13:09:15 +00:00
|
|
|
return err;
|
2010-03-04 18:46:13 +00:00
|
|
|
}
|
2011-03-11 13:51:12 +00:00
|
|
|
|
2018-05-27 13:09:15 +00:00
|
|
|
return snd_usb_autoresume(subs->stream->chip);
|
2010-03-04 18:46:13 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 11:59:03 +00:00
|
|
|
static int snd_usb_pcm_open(struct snd_pcm_substream *substream)
|
2010-03-04 18:46:13 +00:00
|
|
|
{
|
2018-05-27 11:59:03 +00:00
|
|
|
int direction = substream->stream;
|
2010-03-04 18:46:13 +00:00
|
|
|
struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
|
|
|
|
struct snd_pcm_runtime *runtime = substream->runtime;
|
|
|
|
struct snd_usb_substream *subs = &as->substream[direction];
|
media: sound/usb: Use Media Controller API to share media resources
Media Device Allocator API to allows multiple drivers share a media device.
This API solves a very common use-case for media devices where one physical
device (an USB stick) provides both audio and video. When such media device
exposes a standard USB Audio class, a proprietary Video class, two or more
independent drivers will share a single physical USB bridge. In such cases,
it is necessary to coordinate access to the shared resource.
Using this API, drivers can allocate a media device with the shared struct
device as the key. Once the media device is allocated by a driver, other
drivers can get a reference to it. The media device is released when all
the references are released.
Change the ALSA driver to use the Media Controller API to share media
resources with DVB, and V4L2 drivers on a AU0828 media device.
The Media Controller specific initialization is done after sound card is
registered. ALSA creates Media interface and entity function graph nodes
for Control, Mixer, PCM Playback, and PCM Capture devices.
snd_usb_hw_params() will call Media Controller enable source handler
interface to request the media resource. If resource request is granted,
it will release it from snd_usb_hw_free(). If resource is busy, -EBUSY is
returned.
Media specific cleanup is done in usb_audio_disconnect().
Reviewed-by: Takashi Iwai <tiwai@suse.de>
Signed-off-by: Shuah Khan <shuah@kernel.org>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
2019-04-02 00:40:22 +00:00
|
|
|
int ret;
|
2010-03-04 18:46:13 +00:00
|
|
|
|
|
|
|
subs->interface = -1;
|
2010-03-04 18:46:14 +00:00
|
|
|
subs->altset_idx = 0;
|
2010-03-04 18:46:13 +00:00
|
|
|
runtime->hw = snd_usb_hardware;
|
|
|
|
runtime->private_data = subs;
|
|
|
|
subs->pcm_substream = substream;
|
2011-03-11 13:51:12 +00:00
|
|
|
/* runtime PM is also done there */
|
2013-04-16 16:01:38 +00:00
|
|
|
|
|
|
|
/* initialize DSD/DOP context */
|
|
|
|
subs->dsd_dop.byte_idx = 0;
|
|
|
|
subs->dsd_dop.channel = 0;
|
|
|
|
subs->dsd_dop.marker = 1;
|
|
|
|
|
media: sound/usb: Use Media Controller API to share media resources
Media Device Allocator API to allows multiple drivers share a media device.
This API solves a very common use-case for media devices where one physical
device (an USB stick) provides both audio and video. When such media device
exposes a standard USB Audio class, a proprietary Video class, two or more
independent drivers will share a single physical USB bridge. In such cases,
it is necessary to coordinate access to the shared resource.
Using this API, drivers can allocate a media device with the shared struct
device as the key. Once the media device is allocated by a driver, other
drivers can get a reference to it. The media device is released when all
the references are released.
Change the ALSA driver to use the Media Controller API to share media
resources with DVB, and V4L2 drivers on a AU0828 media device.
The Media Controller specific initialization is done after sound card is
registered. ALSA creates Media interface and entity function graph nodes
for Control, Mixer, PCM Playback, and PCM Capture devices.
snd_usb_hw_params() will call Media Controller enable source handler
interface to request the media resource. If resource request is granted,
it will release it from snd_usb_hw_free(). If resource is busy, -EBUSY is
returned.
Media specific cleanup is done in usb_audio_disconnect().
Reviewed-by: Takashi Iwai <tiwai@suse.de>
Signed-off-by: Shuah Khan <shuah@kernel.org>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
2019-04-02 00:40:22 +00:00
|
|
|
ret = setup_hw_info(runtime, subs);
|
|
|
|
if (ret == 0) {
|
|
|
|
ret = snd_media_stream_init(subs, as->pcm, direction);
|
|
|
|
if (ret)
|
|
|
|
snd_usb_autosuspend(subs->stream->chip);
|
|
|
|
}
|
|
|
|
return ret;
|
2010-03-04 18:46:13 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 11:59:03 +00:00
|
|
|
static int snd_usb_pcm_close(struct snd_pcm_substream *substream)
|
2010-03-04 18:46:13 +00:00
|
|
|
{
|
2018-05-27 11:59:03 +00:00
|
|
|
int direction = substream->stream;
|
2010-03-04 18:46:13 +00:00
|
|
|
struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
|
|
|
|
struct snd_usb_substream *subs = &as->substream[direction];
|
2018-07-31 12:28:45 +00:00
|
|
|
int ret;
|
2010-03-04 18:46:13 +00:00
|
|
|
|
media: sound/usb: Use Media Controller API to share media resources
Media Device Allocator API to allows multiple drivers share a media device.
This API solves a very common use-case for media devices where one physical
device (an USB stick) provides both audio and video. When such media device
exposes a standard USB Audio class, a proprietary Video class, two or more
independent drivers will share a single physical USB bridge. In such cases,
it is necessary to coordinate access to the shared resource.
Using this API, drivers can allocate a media device with the shared struct
device as the key. Once the media device is allocated by a driver, other
drivers can get a reference to it. The media device is released when all
the references are released.
Change the ALSA driver to use the Media Controller API to share media
resources with DVB, and V4L2 drivers on a AU0828 media device.
The Media Controller specific initialization is done after sound card is
registered. ALSA creates Media interface and entity function graph nodes
for Control, Mixer, PCM Playback, and PCM Capture devices.
snd_usb_hw_params() will call Media Controller enable source handler
interface to request the media resource. If resource request is granted,
it will release it from snd_usb_hw_free(). If resource is busy, -EBUSY is
returned.
Media specific cleanup is done in usb_audio_disconnect().
Reviewed-by: Takashi Iwai <tiwai@suse.de>
Signed-off-by: Shuah Khan <shuah@kernel.org>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
2019-04-02 00:40:22 +00:00
|
|
|
snd_media_stop_pipeline(subs);
|
2012-07-12 11:08:40 +00:00
|
|
|
|
2018-05-02 08:04:27 +00:00
|
|
|
if (!as->chip->keep_iface &&
|
|
|
|
subs->interface >= 0 &&
|
ALSA: usb-audio: Avoid nested autoresume calls
After the recent fix of runtime PM for USB-audio driver, we got a
lockdep warning like:
=============================================
[ INFO: possible recursive locking detected ]
4.2.0-rc8+ #61 Not tainted
---------------------------------------------
pulseaudio/980 is trying to acquire lock:
(&chip->shutdown_rwsem){.+.+.+}, at: [<ffffffffa0355dac>] snd_usb_autoresume+0x1d/0x52 [snd_usb_audio]
but task is already holding lock:
(&chip->shutdown_rwsem){.+.+.+}, at: [<ffffffffa0355dac>] snd_usb_autoresume+0x1d/0x52 [snd_usb_audio]
This comes from snd_usb_autoresume() invoking down_read() and it's
used in a nested way. Although it's basically safe, per se (as these
are read locks), it's better to reduce such spurious warnings.
The read lock is needed to guarantee the execution of "shutdown"
(cleanup at disconnection) task after all concurrent tasks are
finished. This can be implemented in another better way.
Also, the current check of chip->in_pm isn't good enough for
protecting the racy execution of multiple auto-resumes.
This patch rewrites the logic of snd_usb_autoresume() & co; namely,
- The recursive call of autopm is avoided by the new refcount,
chip->active. The chip->in_pm flag is removed accordingly.
- Instead of rwsem, another refcount, chip->usage_count, is introduced
for tracking the period to delay the shutdown procedure. At
the last clear of this refcount, wake_up() to the shutdown waiter is
called.
- The shutdown flag is replaced with shutdown atomic count; this is
for reducing the lock.
- Two new helpers are introduced to simplify the management of these
refcounts; snd_usb_lock_shutdown() increases the usage_count, checks
the shutdown state, and does autoresume. snd_usb_unlock_shutdown()
does the opposite. Most of mixer and other codes just need this,
and simply returns an error if it receives an error from lock.
Fixes: 9003ebb13f61 ('ALSA: usb-audio: Fix runtime PM unbalance')
Reported-and-tested-by: Alexnader Kuleshov <kuleshovmail@gmail.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2015-08-25 14:09:00 +00:00
|
|
|
!snd_usb_lock_shutdown(subs->stream->chip)) {
|
2012-07-12 11:08:40 +00:00
|
|
|
usb_set_interface(subs->dev, subs->interface, 0);
|
|
|
|
subs->interface = -1;
|
2018-07-31 12:28:45 +00:00
|
|
|
ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D1);
|
ALSA: usb-audio: Avoid nested autoresume calls
After the recent fix of runtime PM for USB-audio driver, we got a
lockdep warning like:
=============================================
[ INFO: possible recursive locking detected ]
4.2.0-rc8+ #61 Not tainted
---------------------------------------------
pulseaudio/980 is trying to acquire lock:
(&chip->shutdown_rwsem){.+.+.+}, at: [<ffffffffa0355dac>] snd_usb_autoresume+0x1d/0x52 [snd_usb_audio]
but task is already holding lock:
(&chip->shutdown_rwsem){.+.+.+}, at: [<ffffffffa0355dac>] snd_usb_autoresume+0x1d/0x52 [snd_usb_audio]
This comes from snd_usb_autoresume() invoking down_read() and it's
used in a nested way. Although it's basically safe, per se (as these
are read locks), it's better to reduce such spurious warnings.
The read lock is needed to guarantee the execution of "shutdown"
(cleanup at disconnection) task after all concurrent tasks are
finished. This can be implemented in another better way.
Also, the current check of chip->in_pm isn't good enough for
protecting the racy execution of multiple auto-resumes.
This patch rewrites the logic of snd_usb_autoresume() & co; namely,
- The recursive call of autopm is avoided by the new refcount,
chip->active. The chip->in_pm flag is removed accordingly.
- Instead of rwsem, another refcount, chip->usage_count, is introduced
for tracking the period to delay the shutdown procedure. At
the last clear of this refcount, wake_up() to the shutdown waiter is
called.
- The shutdown flag is replaced with shutdown atomic count; this is
for reducing the lock.
- Two new helpers are introduced to simplify the management of these
refcounts; snd_usb_lock_shutdown() increases the usage_count, checks
the shutdown state, and does autoresume. snd_usb_unlock_shutdown()
does the opposite. Most of mixer and other codes just need this,
and simply returns an error if it receives an error from lock.
Fixes: 9003ebb13f61 ('ALSA: usb-audio: Fix runtime PM unbalance')
Reported-and-tested-by: Alexnader Kuleshov <kuleshovmail@gmail.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2015-08-25 14:09:00 +00:00
|
|
|
snd_usb_unlock_shutdown(subs->stream->chip);
|
2018-07-31 12:28:45 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-07-12 11:08:40 +00:00
|
|
|
}
|
|
|
|
|
2010-03-04 18:46:13 +00:00
|
|
|
subs->pcm_substream = NULL;
|
2011-03-11 13:51:12 +00:00
|
|
|
snd_usb_autosuspend(subs->stream->chip);
|
2012-04-12 11:51:12 +00:00
|
|
|
|
2012-07-12 11:08:40 +00:00
|
|
|
return 0;
|
2012-04-12 11:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Since a URB can handle only a single linear buffer, we must use double
|
|
|
|
* buffering when the data to be transferred overflows the buffer boundary.
|
|
|
|
* To avoid inconsistencies when updating hwptr_done, we use double buffering
|
|
|
|
* for all URBs.
|
|
|
|
*/
|
|
|
|
static void retire_capture_urb(struct snd_usb_substream *subs,
|
|
|
|
struct urb *urb)
|
|
|
|
{
|
|
|
|
struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
|
|
|
|
unsigned int stride, frames, bytes, oldptr;
|
|
|
|
int i, period_elapsed = 0;
|
|
|
|
unsigned long flags;
|
|
|
|
unsigned char *cp;
|
2012-12-19 17:39:05 +00:00
|
|
|
int current_frame_number;
|
|
|
|
|
|
|
|
/* read frame number here, update pointer in critical section */
|
|
|
|
current_frame_number = usb_get_current_frame_number(subs->dev);
|
2012-04-12 11:51:12 +00:00
|
|
|
|
|
|
|
stride = runtime->frame_bits >> 3;
|
|
|
|
|
|
|
|
for (i = 0; i < urb->number_of_packets; i++) {
|
2013-04-13 03:33:59 +00:00
|
|
|
cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
|
2012-04-12 11:51:12 +00:00
|
|
|
if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
|
2014-02-26 12:02:17 +00:00
|
|
|
dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
|
|
|
|
i, urb->iso_frame_desc[i].status);
|
2012-04-12 11:51:12 +00:00
|
|
|
// continue;
|
|
|
|
}
|
|
|
|
bytes = urb->iso_frame_desc[i].actual_length;
|
2020-08-10 08:24:00 +00:00
|
|
|
if (subs->stream_offset_adj > 0) {
|
|
|
|
unsigned int adj = min(subs->stream_offset_adj, bytes);
|
|
|
|
cp += adj;
|
|
|
|
bytes -= adj;
|
|
|
|
subs->stream_offset_adj -= adj;
|
|
|
|
}
|
2012-04-12 11:51:12 +00:00
|
|
|
frames = bytes / stride;
|
|
|
|
if (!subs->txfr_quirk)
|
|
|
|
bytes = frames * stride;
|
|
|
|
if (bytes % (runtime->sample_bits >> 3) != 0) {
|
|
|
|
int oldbytes = bytes;
|
|
|
|
bytes = frames * stride;
|
2018-05-16 18:07:18 +00:00
|
|
|
dev_warn_ratelimited(&subs->dev->dev,
|
2014-02-26 12:02:17 +00:00
|
|
|
"Corrected urb data len. %d->%d\n",
|
2012-04-12 11:51:12 +00:00
|
|
|
oldbytes, bytes);
|
|
|
|
}
|
|
|
|
/* update the current pointer */
|
|
|
|
spin_lock_irqsave(&subs->lock, flags);
|
|
|
|
oldptr = subs->hwptr_done;
|
|
|
|
subs->hwptr_done += bytes;
|
|
|
|
if (subs->hwptr_done >= runtime->buffer_size * stride)
|
|
|
|
subs->hwptr_done -= runtime->buffer_size * stride;
|
|
|
|
frames = (bytes + (oldptr % stride)) / stride;
|
|
|
|
subs->transfer_done += frames;
|
|
|
|
if (subs->transfer_done >= runtime->period_size) {
|
|
|
|
subs->transfer_done -= runtime->period_size;
|
|
|
|
period_elapsed = 1;
|
|
|
|
}
|
2012-12-19 17:39:05 +00:00
|
|
|
/* capture delay is by construction limited to one URB,
|
|
|
|
* reset delays here
|
|
|
|
*/
|
|
|
|
runtime->delay = subs->last_delay = 0;
|
|
|
|
|
|
|
|
/* realign last_frame_number */
|
|
|
|
subs->last_frame_number = current_frame_number;
|
|
|
|
subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
|
|
|
|
|
2012-04-12 11:51:12 +00:00
|
|
|
spin_unlock_irqrestore(&subs->lock, flags);
|
|
|
|
/* copy a data chunk */
|
|
|
|
if (oldptr + bytes > runtime->buffer_size * stride) {
|
|
|
|
unsigned int bytes1 =
|
|
|
|
runtime->buffer_size * stride - oldptr;
|
|
|
|
memcpy(runtime->dma_area + oldptr, cp, bytes1);
|
|
|
|
memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
|
|
|
|
} else {
|
|
|
|
memcpy(runtime->dma_area + oldptr, cp, bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (period_elapsed)
|
|
|
|
snd_pcm_period_elapsed(subs->pcm_substream);
|
|
|
|
}
|
|
|
|
|
2013-04-16 16:01:38 +00:00
|
|
|
static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
|
|
|
|
struct urb *urb, unsigned int bytes)
|
|
|
|
{
|
|
|
|
struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
|
|
|
|
unsigned int stride = runtime->frame_bits >> 3;
|
|
|
|
unsigned int dst_idx = 0;
|
|
|
|
unsigned int src_idx = subs->hwptr_done;
|
|
|
|
unsigned int wrap = runtime->buffer_size * stride;
|
|
|
|
u8 *dst = urb->transfer_buffer;
|
|
|
|
u8 *src = runtime->dma_area;
|
|
|
|
u8 marker[] = { 0x05, 0xfa };
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The DSP DOP format defines a way to transport DSD samples over
|
|
|
|
* normal PCM data endpoints. It requires stuffing of marker bytes
|
|
|
|
* (0x05 and 0xfa, alternating per sample frame), and then expects
|
|
|
|
* 2 additional bytes of actual payload. The whole frame is stored
|
|
|
|
* LSB.
|
|
|
|
*
|
|
|
|
* Hence, for a stereo transport, the buffer layout looks like this,
|
|
|
|
* where L refers to left channel samples and R to right.
|
|
|
|
*
|
|
|
|
* L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
|
|
|
|
* L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
|
|
|
|
* .....
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (bytes--) {
|
|
|
|
if (++subs->dsd_dop.byte_idx == 3) {
|
|
|
|
/* frame boundary? */
|
|
|
|
dst[dst_idx++] = marker[subs->dsd_dop.marker];
|
|
|
|
src_idx += 2;
|
|
|
|
subs->dsd_dop.byte_idx = 0;
|
|
|
|
|
|
|
|
if (++subs->dsd_dop.channel % runtime->channels == 0) {
|
|
|
|
/* alternate the marker */
|
|
|
|
subs->dsd_dop.marker++;
|
|
|
|
subs->dsd_dop.marker %= ARRAY_SIZE(marker);
|
|
|
|
subs->dsd_dop.channel = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* stuff the DSD payload */
|
|
|
|
int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
|
2013-04-16 16:01:39 +00:00
|
|
|
|
|
|
|
if (subs->cur_audiofmt->dsd_bitrev)
|
|
|
|
dst[dst_idx++] = bitrev8(src[idx]);
|
|
|
|
else
|
|
|
|
dst[dst_idx++] = src[idx];
|
|
|
|
|
2013-04-16 16:01:38 +00:00
|
|
|
subs->hwptr_done++;
|
|
|
|
}
|
|
|
|
}
|
2015-10-19 06:52:50 +00:00
|
|
|
if (subs->hwptr_done >= runtime->buffer_size * stride)
|
|
|
|
subs->hwptr_done -= runtime->buffer_size * stride;
|
2013-04-16 16:01:38 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 06:52:52 +00:00
|
|
|
static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
|
|
|
|
int offset, int stride, unsigned int bytes)
|
2015-10-19 06:52:49 +00:00
|
|
|
{
|
|
|
|
struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
|
|
|
|
|
|
|
|
if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
|
|
|
|
/* err, the transferred area goes over buffer boundary. */
|
|
|
|
unsigned int bytes1 =
|
|
|
|
runtime->buffer_size * stride - subs->hwptr_done;
|
2015-10-19 06:52:52 +00:00
|
|
|
memcpy(urb->transfer_buffer + offset,
|
2015-10-19 06:52:49 +00:00
|
|
|
runtime->dma_area + subs->hwptr_done, bytes1);
|
2015-10-19 06:52:52 +00:00
|
|
|
memcpy(urb->transfer_buffer + offset + bytes1,
|
2015-10-19 06:52:49 +00:00
|
|
|
runtime->dma_area, bytes - bytes1);
|
|
|
|
} else {
|
2015-10-19 06:52:52 +00:00
|
|
|
memcpy(urb->transfer_buffer + offset,
|
2015-10-19 06:52:49 +00:00
|
|
|
runtime->dma_area + subs->hwptr_done, bytes);
|
|
|
|
}
|
|
|
|
subs->hwptr_done += bytes;
|
2015-10-19 06:52:50 +00:00
|
|
|
if (subs->hwptr_done >= runtime->buffer_size * stride)
|
|
|
|
subs->hwptr_done -= runtime->buffer_size * stride;
|
2015-10-19 06:52:49 +00:00
|
|
|
}
|
|
|
|
|
ALSA: USB-audio: Add quirk for Zoom R16/24 playback
The Zoom R16/24 have a nonstandard playback format where each isochronous
packet contains a length descriptor in the first four bytes. (Curiously,
capture data does not contain this and requires no quirk.)
The quirk involves adding the extra length descriptor whenever outgoing
isochronous packets are generated, both in pcm.c (outgoing audio) and
endpoint.c (silent data).
In order to make the quirk as unintrusive as possible, for
pcm.c:prepare_playback_urb(), the isochronous packet descriptors are
initially set up in the same way no matter if the quirk is enabled or not.
Once it is time to actually copy the data into the outgoing packet buffer
(together with the added length descriptors) the isochronous descriptors
are adjusted in order take the increased payload length into account.
For endpoint.c:prepare_silent_urb() it makes more sense to modify the
actual function, partly because the function is less complex to start with
and partly because it is not as time-critical as prepare_playback_urb()
(whose bulk is run with interrupts disabled), so the (minute) additional
time spent in the non-quirk case is motivated by the simplicity of having
a single function for all cases.
The quirk is controlled by the new tx_length_quirk member in struct
snd_usb_substream and struct snd_usb_audio, which is conveyed to pcm.c
and endpoint.c from quirks.c in a similar manner to the txfr_quirk member
in the same structs.
In contrast to txfr_quirk however, the quirk is enabled directly in
quirks.c:create_standard_audio_quirk() by checking the USB ID in that
function. Another option would be to introduce a new
QUIRK_AUDIO_ZOOM_INTERFACE or somesuch, which would have made the quirk
very plain to see in the quirk table, but it was felt that the additional
code needed to implement it this way would just make the implementation
more complex with no real gain.
Tested with a Zoom R16, both by doing capture and playback separately
using arecord and aplay (8 channel capture and 2 channel playback,
respectively), as well as capture and playback together using Ardour, as
well as Audacity and Qtractor together with jackd.
The R24 is reportedly compatible with the R16 when used as an audio
interface. Both devices share the same USB ID and have the same number of
inputs (8) and outputs (2). Therefore "R16/24" is mentioned throughout the
patch.
Regression tested using an Edirol UA-5 in both class compliant (16-bit)
and "advanced" (24 bit, forces the use of quirks) modes.
Signed-off-by: Ricard Wanderlof <ricardw@axis.com>
Tested-by: Panu Matilainen <pmatilai@laiskiainen.org>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2015-10-19 06:52:53 +00:00
|
|
|
static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
|
|
|
|
struct urb *urb, int stride,
|
|
|
|
unsigned int bytes)
|
|
|
|
{
|
|
|
|
__le32 packet_length;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Put __le32 length descriptor at start of each packet. */
|
|
|
|
for (i = 0; i < urb->number_of_packets; i++) {
|
|
|
|
unsigned int length = urb->iso_frame_desc[i].length;
|
|
|
|
unsigned int offset = urb->iso_frame_desc[i].offset;
|
|
|
|
|
|
|
|
packet_length = cpu_to_le32(length);
|
|
|
|
offset += i * sizeof(packet_length);
|
|
|
|
urb->iso_frame_desc[i].offset = offset;
|
|
|
|
urb->iso_frame_desc[i].length += sizeof(packet_length);
|
|
|
|
memcpy(urb->transfer_buffer + offset,
|
|
|
|
&packet_length, sizeof(packet_length));
|
|
|
|
copy_to_urb(subs, urb, offset + sizeof(packet_length),
|
|
|
|
stride, length);
|
|
|
|
}
|
|
|
|
/* Adjust transfer size accordingly. */
|
|
|
|
bytes += urb->number_of_packets * sizeof(packet_length);
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
2012-04-12 11:51:12 +00:00
|
|
|
static void prepare_playback_urb(struct snd_usb_substream *subs,
|
|
|
|
struct urb *urb)
|
|
|
|
{
|
|
|
|
struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
|
2012-08-30 16:52:30 +00:00
|
|
|
struct snd_usb_endpoint *ep = subs->data_endpoint;
|
2012-04-12 11:51:12 +00:00
|
|
|
struct snd_urb_ctx *ctx = urb->context;
|
|
|
|
unsigned int counts, frames, bytes;
|
|
|
|
int i, stride, period_elapsed = 0;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
stride = runtime->frame_bits >> 3;
|
|
|
|
|
|
|
|
frames = 0;
|
|
|
|
urb->number_of_packets = 0;
|
|
|
|
spin_lock_irqsave(&subs->lock, flags);
|
ALSA: improve buffer size computations for USB PCM audio
This patch changes the way URBs are allocated and their sizes are
determined for PCM playback in the snd-usb-audio driver. Currently
the driver allocates too few URBs for endpoints that don't use
implicit sync, making underruns more likely to occur. This may be a
holdover from before I/O delays could be measured accurately; in any
case, it is no longer necessary.
The patch allocates as many URBs as possible, subject to four
limitations:
The total number of URBs for the endpoint is not allowed to
exceed MAX_URBS (which the patch increases from 8 to 12).
The total number of packets per URB is not allowed to exceed
MAX_PACKS (or MAX_PACKS_HS for high-speed devices), which is
decreased from 20 to 6.
The total duration of queued data is not allowed to exceed
MAX_QUEUE, which is decreased from 24 ms to 18 ms.
The total number of ALSA frames in the output queue is not
allowed to exceed the ALSA buffer size.
The last requirement is the hardest to implement. Currently the
number of URBs needed to fill a buffer cannot be determined in
advance, because a buffer contains a fixed number of frames whereas
the number of frames in an URB varies to match shifts in the device's
clock rate. To solve this problem, the patch changes the logic for
deciding how many packets an URB should contain. Rather than using as
many as possible without exceeding an ALSA period boundary, now the
driver uses only as many packets as needed to transfer a predetermined
number of frames. As a result, unless the device's clock has an
exceedingly variable rate, the number of URBs making up each period
(and hence each buffer) will remain constant.
The overall effect of the patch is that playback works better in
low-latency settings. The user can still specify values for
frames/period and periods/buffer that exceed the capabilities of the
hardware, of course. But for values that are within those
capabilities, the performance will be improved. For example, testing
shows that a high-speed device can handle 32 frames/period and 3
periods/buffer at 48 KHz, whereas the current driver starts to get
glitchy at 64 frames/period and 2 periods/buffer.
A side effect of these changes is that the "nrpacks" module parameter
is no longer used. The patch removes it.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
CC: Clemens Ladisch <clemens@ladisch.de>
Tested-by: Daniel Mack <zonque@gmail.com>
Tested-by: Eldad Zack <eldad@fogrefinery.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2013-09-24 19:51:58 +00:00
|
|
|
subs->frame_limit += ep->max_urb_frames;
|
2012-04-12 11:51:12 +00:00
|
|
|
for (i = 0; i < ctx->packets; i++) {
|
2012-08-30 16:52:30 +00:00
|
|
|
if (ctx->packet_size[i])
|
|
|
|
counts = ctx->packet_size[i];
|
2020-04-24 02:24:48 +00:00
|
|
|
else if (ep->sync_master)
|
|
|
|
counts = snd_usb_endpoint_slave_next_packet_size(ep);
|
2012-08-30 16:52:30 +00:00
|
|
|
else
|
|
|
|
counts = snd_usb_endpoint_next_packet_size(ep);
|
|
|
|
|
2012-04-12 11:51:12 +00:00
|
|
|
/* set up descriptor */
|
2013-04-16 16:01:37 +00:00
|
|
|
urb->iso_frame_desc[i].offset = frames * ep->stride;
|
|
|
|
urb->iso_frame_desc[i].length = counts * ep->stride;
|
2012-04-12 11:51:12 +00:00
|
|
|
frames += counts;
|
|
|
|
urb->number_of_packets++;
|
|
|
|
subs->transfer_done += counts;
|
|
|
|
if (subs->transfer_done >= runtime->period_size) {
|
|
|
|
subs->transfer_done -= runtime->period_size;
|
ALSA: improve buffer size computations for USB PCM audio
This patch changes the way URBs are allocated and their sizes are
determined for PCM playback in the snd-usb-audio driver. Currently
the driver allocates too few URBs for endpoints that don't use
implicit sync, making underruns more likely to occur. This may be a
holdover from before I/O delays could be measured accurately; in any
case, it is no longer necessary.
The patch allocates as many URBs as possible, subject to four
limitations:
The total number of URBs for the endpoint is not allowed to
exceed MAX_URBS (which the patch increases from 8 to 12).
The total number of packets per URB is not allowed to exceed
MAX_PACKS (or MAX_PACKS_HS for high-speed devices), which is
decreased from 20 to 6.
The total duration of queued data is not allowed to exceed
MAX_QUEUE, which is decreased from 24 ms to 18 ms.
The total number of ALSA frames in the output queue is not
allowed to exceed the ALSA buffer size.
The last requirement is the hardest to implement. Currently the
number of URBs needed to fill a buffer cannot be determined in
advance, because a buffer contains a fixed number of frames whereas
the number of frames in an URB varies to match shifts in the device's
clock rate. To solve this problem, the patch changes the logic for
deciding how many packets an URB should contain. Rather than using as
many as possible without exceeding an ALSA period boundary, now the
driver uses only as many packets as needed to transfer a predetermined
number of frames. As a result, unless the device's clock has an
exceedingly variable rate, the number of URBs making up each period
(and hence each buffer) will remain constant.
The overall effect of the patch is that playback works better in
low-latency settings. The user can still specify values for
frames/period and periods/buffer that exceed the capabilities of the
hardware, of course. But for values that are within those
capabilities, the performance will be improved. For example, testing
shows that a high-speed device can handle 32 frames/period and 3
periods/buffer at 48 KHz, whereas the current driver starts to get
glitchy at 64 frames/period and 2 periods/buffer.
A side effect of these changes is that the "nrpacks" module parameter
is no longer used. The patch removes it.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
CC: Clemens Ladisch <clemens@ladisch.de>
Tested-by: Daniel Mack <zonque@gmail.com>
Tested-by: Eldad Zack <eldad@fogrefinery.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2013-09-24 19:51:58 +00:00
|
|
|
subs->frame_limit = 0;
|
2012-04-12 11:51:12 +00:00
|
|
|
period_elapsed = 1;
|
|
|
|
if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
|
|
|
|
if (subs->transfer_done > 0) {
|
|
|
|
/* FIXME: fill-max mode is not
|
|
|
|
* supported yet */
|
|
|
|
frames -= subs->transfer_done;
|
|
|
|
counts -= subs->transfer_done;
|
|
|
|
urb->iso_frame_desc[i].length =
|
2013-04-16 16:01:37 +00:00
|
|
|
counts * ep->stride;
|
2012-04-12 11:51:12 +00:00
|
|
|
subs->transfer_done = 0;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
if (i < ctx->packets) {
|
|
|
|
/* add a transfer delimiter */
|
|
|
|
urb->iso_frame_desc[i].offset =
|
2013-04-16 16:01:37 +00:00
|
|
|
frames * ep->stride;
|
2012-04-12 11:51:12 +00:00
|
|
|
urb->iso_frame_desc[i].length = 0;
|
|
|
|
urb->number_of_packets++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
ALSA: improve buffer size computations for USB PCM audio
This patch changes the way URBs are allocated and their sizes are
determined for PCM playback in the snd-usb-audio driver. Currently
the driver allocates too few URBs for endpoints that don't use
implicit sync, making underruns more likely to occur. This may be a
holdover from before I/O delays could be measured accurately; in any
case, it is no longer necessary.
The patch allocates as many URBs as possible, subject to four
limitations:
The total number of URBs for the endpoint is not allowed to
exceed MAX_URBS (which the patch increases from 8 to 12).
The total number of packets per URB is not allowed to exceed
MAX_PACKS (or MAX_PACKS_HS for high-speed devices), which is
decreased from 20 to 6.
The total duration of queued data is not allowed to exceed
MAX_QUEUE, which is decreased from 24 ms to 18 ms.
The total number of ALSA frames in the output queue is not
allowed to exceed the ALSA buffer size.
The last requirement is the hardest to implement. Currently the
number of URBs needed to fill a buffer cannot be determined in
advance, because a buffer contains a fixed number of frames whereas
the number of frames in an URB varies to match shifts in the device's
clock rate. To solve this problem, the patch changes the logic for
deciding how many packets an URB should contain. Rather than using as
many as possible without exceeding an ALSA period boundary, now the
driver uses only as many packets as needed to transfer a predetermined
number of frames. As a result, unless the device's clock has an
exceedingly variable rate, the number of URBs making up each period
(and hence each buffer) will remain constant.
The overall effect of the patch is that playback works better in
low-latency settings. The user can still specify values for
frames/period and periods/buffer that exceed the capabilities of the
hardware, of course. But for values that are within those
capabilities, the performance will be improved. For example, testing
shows that a high-speed device can handle 32 frames/period and 3
periods/buffer at 48 KHz, whereas the current driver starts to get
glitchy at 64 frames/period and 2 periods/buffer.
A side effect of these changes is that the "nrpacks" module parameter
is no longer used. The patch removes it.
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
CC: Clemens Ladisch <clemens@ladisch.de>
Tested-by: Daniel Mack <zonque@gmail.com>
Tested-by: Eldad Zack <eldad@fogrefinery.com>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2013-09-24 19:51:58 +00:00
|
|
|
/* finish at the period boundary or after enough frames */
|
|
|
|
if ((period_elapsed ||
|
|
|
|
subs->transfer_done >= subs->frame_limit) &&
|
|
|
|
!snd_usb_endpoint_implicit_feedback_sink(ep))
|
2012-04-12 11:51:12 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-04-16 16:01:37 +00:00
|
|
|
bytes = frames * ep->stride;
|
2013-04-16 16:01:38 +00:00
|
|
|
|
|
|
|
if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
|
|
|
|
subs->cur_audiofmt->dsd_dop)) {
|
|
|
|
fill_playback_urb_dsd_dop(subs, urb, bytes);
|
2013-04-16 16:01:39 +00:00
|
|
|
} else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
|
|
|
|
subs->cur_audiofmt->dsd_bitrev)) {
|
|
|
|
/* bit-reverse the bytes */
|
|
|
|
u8 *buf = urb->transfer_buffer;
|
|
|
|
for (i = 0; i < bytes; i++) {
|
|
|
|
int idx = (subs->hwptr_done + i)
|
|
|
|
% (runtime->buffer_size * stride);
|
|
|
|
buf[i] = bitrev8(runtime->dma_area[idx]);
|
|
|
|
}
|
|
|
|
|
|
|
|
subs->hwptr_done += bytes;
|
2015-10-19 06:52:50 +00:00
|
|
|
if (subs->hwptr_done >= runtime->buffer_size * stride)
|
|
|
|
subs->hwptr_done -= runtime->buffer_size * stride;
|
2012-04-12 11:51:12 +00:00
|
|
|
} else {
|
2013-04-16 16:01:38 +00:00
|
|
|
/* usual PCM */
|
ALSA: USB-audio: Add quirk for Zoom R16/24 playback
The Zoom R16/24 have a nonstandard playback format where each isochronous
packet contains a length descriptor in the first four bytes. (Curiously,
capture data does not contain this and requires no quirk.)
The quirk involves adding the extra length descriptor whenever outgoing
isochronous packets are generated, both in pcm.c (outgoing audio) and
endpoint.c (silent data).
In order to make the quirk as unintrusive as possible, for
pcm.c:prepare_playback_urb(), the isochronous packet descriptors are
initially set up in the same way no matter if the quirk is enabled or not.
Once it is time to actually copy the data into the outgoing packet buffer
(together with the added length descriptors) the isochronous descriptors
are adjusted in order take the increased payload length into account.
For endpoint.c:prepare_silent_urb() it makes more sense to modify the
actual function, partly because the function is less complex to start with
and partly because it is not as time-critical as prepare_playback_urb()
(whose bulk is run with interrupts disabled), so the (minute) additional
time spent in the non-quirk case is motivated by the simplicity of having
a single function for all cases.
The quirk is controlled by the new tx_length_quirk member in struct
snd_usb_substream and struct snd_usb_audio, which is conveyed to pcm.c
and endpoint.c from quirks.c in a similar manner to the txfr_quirk member
in the same structs.
In contrast to txfr_quirk however, the quirk is enabled directly in
quirks.c:create_standard_audio_quirk() by checking the USB ID in that
function. Another option would be to introduce a new
QUIRK_AUDIO_ZOOM_INTERFACE or somesuch, which would have made the quirk
very plain to see in the quirk table, but it was felt that the additional
code needed to implement it this way would just make the implementation
more complex with no real gain.
Tested with a Zoom R16, both by doing capture and playback separately
using arecord and aplay (8 channel capture and 2 channel playback,
respectively), as well as capture and playback together using Ardour, as
well as Audacity and Qtractor together with jackd.
The R24 is reportedly compatible with the R16 when used as an audio
interface. Both devices share the same USB ID and have the same number of
inputs (8) and outputs (2). Therefore "R16/24" is mentioned throughout the
patch.
Regression tested using an Edirol UA-5 in both class compliant (16-bit)
and "advanced" (24 bit, forces the use of quirks) modes.
Signed-off-by: Ricard Wanderlof <ricardw@axis.com>
Tested-by: Panu Matilainen <pmatilai@laiskiainen.org>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2015-10-19 06:52:53 +00:00
|
|
|
if (!subs->tx_length_quirk)
|
|
|
|
copy_to_urb(subs, urb, 0, stride, bytes);
|
|
|
|
else
|
|
|
|
bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
|
|
|
|
/* bytes is now amount of outgoing data */
|
2012-04-12 11:51:12 +00:00
|
|
|
}
|
2013-04-16 16:01:38 +00:00
|
|
|
|
2012-08-30 16:52:29 +00:00
|
|
|
/* update delay with exact number of samples queued */
|
|
|
|
runtime->delay = subs->last_delay;
|
2012-04-12 11:51:12 +00:00
|
|
|
runtime->delay += frames;
|
2012-08-30 16:52:29 +00:00
|
|
|
subs->last_delay = runtime->delay;
|
|
|
|
|
|
|
|
/* realign last_frame_number */
|
|
|
|
subs->last_frame_number = usb_get_current_frame_number(subs->dev);
|
|
|
|
subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
|
|
|
|
|
2015-02-06 21:55:53 +00:00
|
|
|
if (subs->trigger_tstamp_pending_update) {
|
|
|
|
/* this is the first actual URB submitted,
|
|
|
|
* update trigger timestamp to reflect actual start time
|
|
|
|
*/
|
|
|
|
snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
|
|
|
|
subs->trigger_tstamp_pending_update = false;
|
|
|
|
}
|
|
|
|
|
2012-04-12 11:51:12 +00:00
|
|
|
spin_unlock_irqrestore(&subs->lock, flags);
|
|
|
|
urb->transfer_buffer_length = bytes;
|
|
|
|
if (period_elapsed)
|
|
|
|
snd_pcm_period_elapsed(subs->pcm_substream);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* process after playback data complete
|
|
|
|
* - decrease the delay count again
|
|
|
|
*/
|
|
|
|
static void retire_playback_urb(struct snd_usb_substream *subs,
|
|
|
|
struct urb *urb)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
|
2013-04-16 16:01:37 +00:00
|
|
|
struct snd_usb_endpoint *ep = subs->data_endpoint;
|
|
|
|
int processed = urb->transfer_buffer_length / ep->stride;
|
2012-08-30 16:52:29 +00:00
|
|
|
int est_delay;
|
2012-04-12 11:51:12 +00:00
|
|
|
|
2020-06-29 03:26:07 +00:00
|
|
|
/* ignore the delay accounting when processed=0 is given, i.e.
|
|
|
|
* silent payloads are processed before handling the actual data
|
2012-09-06 12:58:00 +00:00
|
|
|
*/
|
|
|
|
if (!processed)
|
|
|
|
return;
|
|
|
|
|
2012-04-12 11:51:12 +00:00
|
|
|
spin_lock_irqsave(&subs->lock, flags);
|
2012-11-23 15:00:37 +00:00
|
|
|
if (!subs->last_delay)
|
|
|
|
goto out; /* short path */
|
|
|
|
|
2012-08-30 16:52:29 +00:00
|
|
|
est_delay = snd_usb_pcm_delay(subs, runtime->rate);
|
|
|
|
/* update delay with exact number of samples played */
|
|
|
|
if (processed > subs->last_delay)
|
|
|
|
subs->last_delay = 0;
|
2012-04-12 11:51:12 +00:00
|
|
|
else
|
2012-08-30 16:52:29 +00:00
|
|
|
subs->last_delay -= processed;
|
|
|
|
runtime->delay = subs->last_delay;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Report when delay estimate is off by more than 2ms.
|
|
|
|
* The error should be lower than 2ms since the estimate relies
|
|
|
|
* on two reads of a counter updated every ms.
|
|
|
|
*/
|
2014-05-02 13:09:27 +00:00
|
|
|
if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
|
|
|
|
dev_dbg_ratelimited(&subs->dev->dev,
|
2014-02-26 12:02:17 +00:00
|
|
|
"delay: estimated %d, actual %d\n",
|
2012-08-30 16:52:29 +00:00
|
|
|
est_delay, subs->last_delay);
|
|
|
|
|
2012-11-23 15:00:37 +00:00
|
|
|
if (!subs->running) {
|
|
|
|
/* update last_frame_number for delay counting here since
|
|
|
|
* prepare_playback_urb won't be called during pause
|
|
|
|
*/
|
|
|
|
subs->last_frame_number =
|
|
|
|
usb_get_current_frame_number(subs->dev) & 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2012-04-12 11:51:12 +00:00
|
|
|
spin_unlock_irqrestore(&subs->lock, flags);
|
2010-03-04 18:46:13 +00:00
|
|
|
}
|
|
|
|
|
2012-04-12 11:51:12 +00:00
|
|
|
static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
|
|
|
|
int cmd)
|
|
|
|
{
|
|
|
|
struct snd_usb_substream *subs = substream->runtime->private_data;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case SNDRV_PCM_TRIGGER_START:
|
2015-02-06 21:55:53 +00:00
|
|
|
subs->trigger_tstamp_pending_update = true;
|
2020-07-08 20:32:36 +00:00
|
|
|
fallthrough;
|
2012-04-12 11:51:12 +00:00
|
|
|
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
|
|
|
|
subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
|
|
|
|
subs->data_endpoint->retire_data_urb = retire_playback_urb;
|
2012-05-21 10:47:36 +00:00
|
|
|
subs->running = 1;
|
2012-04-12 11:51:12 +00:00
|
|
|
return 0;
|
|
|
|
case SNDRV_PCM_TRIGGER_STOP:
|
2019-12-10 06:34:54 +00:00
|
|
|
stop_endpoints(subs);
|
2012-05-21 10:47:36 +00:00
|
|
|
subs->running = 0;
|
2012-04-12 11:51:12 +00:00
|
|
|
return 0;
|
|
|
|
case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
|
|
|
|
subs->data_endpoint->prepare_data_urb = NULL;
|
2012-11-23 15:00:37 +00:00
|
|
|
/* keep retire_data_urb for delay calculation */
|
|
|
|
subs->data_endpoint->retire_data_urb = retire_playback_urb;
|
2012-05-21 10:47:36 +00:00
|
|
|
subs->running = 0;
|
2012-04-12 11:51:12 +00:00
|
|
|
return 0;
|
2019-12-18 13:26:50 +00:00
|
|
|
case SNDRV_PCM_TRIGGER_SUSPEND:
|
|
|
|
if (subs->stream->chip->setup_fmt_after_resume_quirk) {
|
2019-12-18 19:05:39 +00:00
|
|
|
stop_endpoints(subs);
|
2019-12-18 13:26:50 +00:00
|
|
|
subs->need_setup_fmt = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2012-04-12 11:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2012-06-16 14:58:04 +00:00
|
|
|
static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
|
|
|
|
int cmd)
|
2012-04-12 11:51:12 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct snd_usb_substream *subs = substream->runtime->private_data;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case SNDRV_PCM_TRIGGER_START:
|
2017-01-04 22:37:46 +00:00
|
|
|
err = start_endpoints(subs);
|
2012-04-12 11:51:12 +00:00
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
subs->data_endpoint->retire_data_urb = retire_capture_urb;
|
2012-05-21 10:47:36 +00:00
|
|
|
subs->running = 1;
|
2012-04-12 11:51:12 +00:00
|
|
|
return 0;
|
|
|
|
case SNDRV_PCM_TRIGGER_STOP:
|
2019-12-10 06:34:54 +00:00
|
|
|
stop_endpoints(subs);
|
2020-06-16 12:09:21 +00:00
|
|
|
subs->data_endpoint->retire_data_urb = NULL;
|
2012-05-21 10:47:36 +00:00
|
|
|
subs->running = 0;
|
2012-04-12 11:51:12 +00:00
|
|
|
return 0;
|
|
|
|
case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
|
|
|
|
subs->data_endpoint->retire_data_urb = NULL;
|
2012-05-21 10:47:36 +00:00
|
|
|
subs->running = 0;
|
2012-04-12 11:51:12 +00:00
|
|
|
return 0;
|
|
|
|
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
|
|
|
|
subs->data_endpoint->retire_data_urb = retire_capture_urb;
|
2012-05-21 10:47:36 +00:00
|
|
|
subs->running = 1;
|
2012-04-12 11:51:12 +00:00
|
|
|
return 0;
|
2019-12-18 13:26:50 +00:00
|
|
|
case SNDRV_PCM_TRIGGER_SUSPEND:
|
|
|
|
if (subs->stream->chip->setup_fmt_after_resume_quirk) {
|
2019-12-18 19:05:39 +00:00
|
|
|
stop_endpoints(subs);
|
2019-12-18 13:26:50 +00:00
|
|
|
subs->need_setup_fmt = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2012-04-12 11:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-08-18 07:45:21 +00:00
|
|
|
static const struct snd_pcm_ops snd_usb_playback_ops = {
|
2018-05-27 11:59:03 +00:00
|
|
|
.open = snd_usb_pcm_open,
|
|
|
|
.close = snd_usb_pcm_close,
|
2010-03-04 18:46:13 +00:00
|
|
|
.hw_params = snd_usb_hw_params,
|
|
|
|
.hw_free = snd_usb_hw_free,
|
|
|
|
.prepare = snd_usb_pcm_prepare,
|
|
|
|
.trigger = snd_usb_substream_playback_trigger,
|
2019-12-10 06:34:54 +00:00
|
|
|
.sync_stop = snd_usb_pcm_sync_stop,
|
2010-03-04 18:46:13 +00:00
|
|
|
.pointer = snd_usb_pcm_pointer,
|
|
|
|
};
|
|
|
|
|
2017-08-18 07:45:21 +00:00
|
|
|
static const struct snd_pcm_ops snd_usb_capture_ops = {
|
2018-05-27 11:59:03 +00:00
|
|
|
.open = snd_usb_pcm_open,
|
|
|
|
.close = snd_usb_pcm_close,
|
2010-03-04 18:46:13 +00:00
|
|
|
.hw_params = snd_usb_hw_params,
|
|
|
|
.hw_free = snd_usb_hw_free,
|
|
|
|
.prepare = snd_usb_pcm_prepare,
|
|
|
|
.trigger = snd_usb_substream_capture_trigger,
|
2019-12-10 06:34:54 +00:00
|
|
|
.sync_stop = snd_usb_pcm_sync_stop,
|
2010-03-04 18:46:13 +00:00
|
|
|
.pointer = snd_usb_pcm_pointer,
|
2018-05-27 11:01:17 +00:00
|
|
|
};
|
|
|
|
|
2010-03-04 18:46:13 +00:00
|
|
|
void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
|
|
|
|
{
|
2018-05-27 11:01:17 +00:00
|
|
|
const struct snd_pcm_ops *ops;
|
|
|
|
|
2019-11-05 15:18:40 +00:00
|
|
|
ops = stream == SNDRV_PCM_STREAM_PLAYBACK ?
|
2018-05-27 11:01:17 +00:00
|
|
|
&snd_usb_playback_ops : &snd_usb_capture_ops;
|
|
|
|
snd_pcm_set_ops(pcm, stream, ops);
|
|
|
|
}
|
|
|
|
|
|
|
|
void snd_usb_preallocate_buffer(struct snd_usb_substream *subs)
|
|
|
|
{
|
|
|
|
struct snd_pcm *pcm = subs->stream->pcm;
|
|
|
|
struct snd_pcm_substream *s = pcm->streams[subs->direction].substream;
|
|
|
|
struct device *dev = subs->dev->bus->controller;
|
|
|
|
|
2019-11-05 15:18:40 +00:00
|
|
|
if (snd_usb_use_vmalloc)
|
2019-12-09 09:49:42 +00:00
|
|
|
snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_VMALLOC,
|
|
|
|
NULL, 0, 0);
|
2019-11-05 15:18:40 +00:00
|
|
|
else
|
2019-12-09 09:49:42 +00:00
|
|
|
snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_DEV_SG,
|
|
|
|
dev, 64*1024, 512*1024);
|
2010-03-04 18:46:13 +00:00
|
|
|
}
|