Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 2 | /* |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | #include <linux/init.h> |
Stephen Rothwell | 9966dda | 2010-03-29 19:01:48 +1100 | [diff] [blame] | 6 | #include <linux/slab.h> |
Daniel Mack | 44dcbbb | 2013-04-17 00:01:39 +0800 | [diff] [blame] | 7 | #include <linux/bitrev.h> |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 8 | #include <linux/ratelimit.h> |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 9 | #include <linux/usb.h> |
| 10 | #include <linux/usb/audio.h> |
Daniel Mack | 7e84789 | 2010-03-11 21:13:20 +0100 | [diff] [blame] | 11 | #include <linux/usb/audio-v2.h> |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 12 | |
| 13 | #include <sound/core.h> |
| 14 | #include <sound/pcm.h> |
| 15 | #include <sound/pcm_params.h> |
| 16 | |
| 17 | #include "usbaudio.h" |
| 18 | #include "card.h" |
| 19 | #include "quirks.h" |
Daniel Mack | c731bc9 | 2011-09-14 12:46:57 +0200 | [diff] [blame] | 20 | #include "endpoint.h" |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 21 | #include "helper.h" |
| 22 | #include "pcm.h" |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 23 | #include "clock.h" |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 24 | #include "power.h" |
Shuah Khan | 66354f1 | 2019-04-01 20:40:22 -0400 | [diff] [blame] | 25 | #include "media.h" |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 26 | |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 27 | #define SUBSTREAM_FLAG_DATA_EP_STARTED 0 |
| 28 | #define SUBSTREAM_FLAG_SYNC_EP_STARTED 1 |
| 29 | |
Pierre-Louis Bossart | 294c4fb | 2011-09-06 19:15:34 -0500 | [diff] [blame] | 30 | /* return the estimated delay based on USB frame counters */ |
| 31 | snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs, |
| 32 | unsigned int rate) |
| 33 | { |
| 34 | int current_frame_number; |
| 35 | int frame_diff; |
| 36 | int est_delay; |
| 37 | |
Takashi Iwai | 48779a0 | 2012-11-23 16:00:37 +0100 | [diff] [blame] | 38 | if (!subs->last_delay) |
| 39 | return 0; /* short path */ |
| 40 | |
Pierre-Louis Bossart | 294c4fb | 2011-09-06 19:15:34 -0500 | [diff] [blame] | 41 | current_frame_number = usb_get_current_frame_number(subs->dev); |
| 42 | /* |
| 43 | * HCD implementations use different widths, use lower 8 bits. |
| 44 | * The delay will be managed up to 256ms, which is more than |
| 45 | * enough |
| 46 | */ |
| 47 | frame_diff = (current_frame_number - subs->last_frame_number) & 0xff; |
| 48 | |
| 49 | /* Approximation based on number of samples per USB frame (ms), |
| 50 | some truncation for 44.1 but the estimate is good enough */ |
Pierre-Louis Bossart | e4cc615 | 2012-12-19 11:39:05 -0600 | [diff] [blame] | 51 | est_delay = frame_diff * rate / 1000; |
| 52 | if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) |
| 53 | est_delay = subs->last_delay - est_delay; |
| 54 | else |
| 55 | est_delay = subs->last_delay + est_delay; |
| 56 | |
Pierre-Louis Bossart | 294c4fb | 2011-09-06 19:15:34 -0500 | [diff] [blame] | 57 | if (est_delay < 0) |
| 58 | est_delay = 0; |
| 59 | return est_delay; |
| 60 | } |
| 61 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 62 | /* |
| 63 | * return the current pcm pointer. just based on the hwptr_done value. |
| 64 | */ |
| 65 | static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream) |
| 66 | { |
Takashi Iwai | e92be81 | 2018-05-27 15:09:15 +0200 | [diff] [blame] | 67 | struct snd_usb_substream *subs = substream->runtime->private_data; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 68 | unsigned int hwptr_done; |
| 69 | |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 70 | if (atomic_read(&subs->stream->chip->shutdown)) |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 71 | return SNDRV_PCM_POS_XRUN; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 72 | spin_lock(&subs->lock); |
| 73 | hwptr_done = subs->hwptr_done; |
Pierre-Louis Bossart | e4cc615 | 2012-12-19 11:39:05 -0600 | [diff] [blame] | 74 | substream->runtime->delay = snd_usb_pcm_delay(subs, |
Pierre-Louis Bossart | 294c4fb | 2011-09-06 19:15:34 -0500 | [diff] [blame] | 75 | substream->runtime->rate); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 76 | spin_unlock(&subs->lock); |
| 77 | return hwptr_done / (substream->runtime->frame_bits >> 3); |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * find a matching audio format |
| 82 | */ |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 83 | static struct audioformat *find_format(struct list_head *fmt_list_head, |
| 84 | snd_pcm_format_t format, |
| 85 | unsigned int rate, |
| 86 | unsigned int channels, |
| 87 | struct snd_usb_substream *subs) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 88 | { |
Eldad Zack | 88766f0 | 2013-04-03 23:18:49 +0200 | [diff] [blame] | 89 | struct audioformat *fp; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 90 | struct audioformat *found = NULL; |
| 91 | int cur_attr = 0, attr; |
| 92 | |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 93 | list_for_each_entry(fp, fmt_list_head, list) { |
| 94 | if (!(fp->formats & pcm_format_to_bits(format))) |
Clemens Ladisch | 015eb0b | 2010-03-04 19:46:15 +0100 | [diff] [blame] | 95 | continue; |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 96 | if (fp->channels != channels) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 97 | continue; |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 98 | if (rate < fp->rate_min || rate > fp->rate_max) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 99 | continue; |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 100 | if (!(fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 101 | unsigned int i; |
| 102 | for (i = 0; i < fp->nr_rates; i++) |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 103 | if (fp->rate_table[i] == rate) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 104 | break; |
| 105 | if (i >= fp->nr_rates) |
| 106 | continue; |
| 107 | } |
| 108 | attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE; |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 109 | if (!found) { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 110 | found = fp; |
| 111 | cur_attr = attr; |
| 112 | continue; |
| 113 | } |
| 114 | /* avoid async out and adaptive in if the other method |
| 115 | * supports the same format. |
| 116 | * this is a workaround for the case like |
| 117 | * M-audio audiophile USB. |
| 118 | */ |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 119 | if (subs && attr != cur_attr) { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 120 | if ((attr == USB_ENDPOINT_SYNC_ASYNC && |
| 121 | subs->direction == SNDRV_PCM_STREAM_PLAYBACK) || |
| 122 | (attr == USB_ENDPOINT_SYNC_ADAPTIVE && |
| 123 | subs->direction == SNDRV_PCM_STREAM_CAPTURE)) |
| 124 | continue; |
| 125 | if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC && |
| 126 | subs->direction == SNDRV_PCM_STREAM_PLAYBACK) || |
| 127 | (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE && |
| 128 | subs->direction == SNDRV_PCM_STREAM_CAPTURE)) { |
| 129 | found = fp; |
| 130 | cur_attr = attr; |
| 131 | continue; |
| 132 | } |
| 133 | } |
| 134 | /* find the format with the largest max. packet size */ |
| 135 | if (fp->maxpacksize > found->maxpacksize) { |
| 136 | found = fp; |
| 137 | cur_attr = attr; |
| 138 | } |
| 139 | } |
| 140 | return found; |
| 141 | } |
| 142 | |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 143 | static struct audioformat *find_substream_format(struct snd_usb_substream *subs) |
| 144 | { |
| 145 | return find_format(&subs->fmt_list, subs->pcm_format, subs->cur_rate, |
| 146 | subs->channels, subs); |
| 147 | } |
| 148 | |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 149 | static int init_pitch_v1(struct snd_usb_audio *chip, int iface, |
| 150 | struct usb_host_interface *alts, |
| 151 | struct audioformat *fmt) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 152 | { |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 153 | struct usb_device *dev = chip->dev; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 154 | unsigned int ep; |
| 155 | unsigned char data[1]; |
| 156 | int err; |
| 157 | |
Takashi Iwai | 447d627 | 2016-03-15 15:20:58 +0100 | [diff] [blame] | 158 | if (get_iface_desc(alts)->bNumEndpoints < 1) |
| 159 | return -EINVAL; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 160 | ep = get_endpoint(alts, 0)->bEndpointAddress; |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 161 | |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 162 | data[0] = 1; |
Takashi Iwai | f25ecf8 | 2018-05-27 15:18:22 +0200 | [diff] [blame] | 163 | err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR, |
| 164 | USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, |
| 165 | UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep, |
| 166 | data, sizeof(data)); |
| 167 | if (err < 0) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 168 | usb_audio_err(chip, "%d:%d: cannot set enable PITCH\n", |
| 169 | iface, ep); |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 170 | return err; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 171 | } |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 172 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 173 | return 0; |
| 174 | } |
| 175 | |
Daniel Mack | 92c2561 | 2010-05-26 18:11:39 +0200 | [diff] [blame] | 176 | static int init_pitch_v2(struct snd_usb_audio *chip, int iface, |
| 177 | struct usb_host_interface *alts, |
| 178 | struct audioformat *fmt) |
| 179 | { |
| 180 | struct usb_device *dev = chip->dev; |
| 181 | unsigned char data[1]; |
Daniel Mack | 92c2561 | 2010-05-26 18:11:39 +0200 | [diff] [blame] | 182 | int err; |
| 183 | |
Daniel Mack | 92c2561 | 2010-05-26 18:11:39 +0200 | [diff] [blame] | 184 | data[0] = 1; |
Takashi Iwai | f25ecf8 | 2018-05-27 15:18:22 +0200 | [diff] [blame] | 185 | err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR, |
| 186 | USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT, |
| 187 | UAC2_EP_CS_PITCH << 8, 0, |
| 188 | data, sizeof(data)); |
| 189 | if (err < 0) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 190 | usb_audio_err(chip, "%d:%d: cannot set enable PITCH (v2)\n", |
| 191 | iface, fmt->altsetting); |
Daniel Mack | 92c2561 | 2010-05-26 18:11:39 +0200 | [diff] [blame] | 192 | return err; |
| 193 | } |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 198 | /* |
Daniel Mack | 92c2561 | 2010-05-26 18:11:39 +0200 | [diff] [blame] | 199 | * initialize the pitch control and sample rate |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 200 | */ |
| 201 | int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface, |
| 202 | struct usb_host_interface *alts, |
| 203 | struct audioformat *fmt) |
| 204 | { |
Daniel Mack | 92c2561 | 2010-05-26 18:11:39 +0200 | [diff] [blame] | 205 | /* if endpoint doesn't have pitch control, bail out */ |
| 206 | if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL)) |
| 207 | return 0; |
| 208 | |
Clemens Ladisch | 8f898e9 | 2013-01-31 21:39:17 +0100 | [diff] [blame] | 209 | switch (fmt->protocol) { |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 210 | case UAC_VERSION_1: |
Clemens Ladisch | a2acad8 | 2010-09-03 10:53:11 +0200 | [diff] [blame] | 211 | default: |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 212 | return init_pitch_v1(chip, iface, alts, fmt); |
| 213 | |
| 214 | case UAC_VERSION_2: |
Daniel Mack | 92c2561 | 2010-05-26 18:11:39 +0200 | [diff] [blame] | 215 | return init_pitch_v2(chip, iface, alts, fmt); |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 216 | } |
Daniel Mack | 767d75a | 2010-03-04 19:46:17 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Ioan-Adrian Ratiu | 1d0f953 | 2017-01-05 00:37:46 +0200 | [diff] [blame] | 219 | static int start_endpoints(struct snd_usb_substream *subs) |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 220 | { |
| 221 | int err; |
| 222 | |
| 223 | if (!subs->data_endpoint) |
| 224 | return -EINVAL; |
| 225 | |
| 226 | if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) { |
| 227 | struct snd_usb_endpoint *ep = subs->data_endpoint; |
| 228 | |
Takashi Iwai | e93e890 | 2020-11-23 09:53:13 +0100 | [diff] [blame] | 229 | dev_dbg(&subs->dev->dev, "Starting data EP 0x%x\n", ep->ep_num); |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 230 | |
| 231 | ep->data_subs = subs; |
Ioan-Adrian Ratiu | 1d0f953 | 2017-01-05 00:37:46 +0200 | [diff] [blame] | 232 | err = snd_usb_endpoint_start(ep); |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 233 | if (err < 0) { |
| 234 | clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags); |
| 235 | return err; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | if (subs->sync_endpoint && |
| 240 | !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) { |
| 241 | struct snd_usb_endpoint *ep = subs->sync_endpoint; |
| 242 | |
Takashi Iwai | e93e890 | 2020-11-23 09:53:13 +0100 | [diff] [blame] | 243 | dev_dbg(&subs->dev->dev, "Starting sync EP 0x%x\n", ep->ep_num); |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 244 | |
| 245 | ep->sync_slave = subs->data_endpoint; |
Ioan-Adrian Ratiu | 1d0f953 | 2017-01-05 00:37:46 +0200 | [diff] [blame] | 246 | err = snd_usb_endpoint_start(ep); |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 247 | if (err < 0) { |
| 248 | clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags); |
Takashi Iwai | 1803503 | 2020-11-23 09:53:12 +0100 | [diff] [blame] | 249 | ep->sync_slave = NULL; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 250 | return err; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
Takashi Iwai | dc5eafe | 2019-12-10 07:34:54 +0100 | [diff] [blame] | 257 | static void sync_pending_stops(struct snd_usb_substream *subs) |
| 258 | { |
| 259 | snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint); |
| 260 | snd_usb_endpoint_sync_pending_stop(subs->data_endpoint); |
| 261 | } |
| 262 | |
| 263 | static void stop_endpoints(struct snd_usb_substream *subs) |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 264 | { |
Takashi Iwai | 1803503 | 2020-11-23 09:53:12 +0100 | [diff] [blame] | 265 | if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) { |
Takashi Iwai | e93e890 | 2020-11-23 09:53:13 +0100 | [diff] [blame] | 266 | dev_dbg(&subs->dev->dev, "Stopping sync EP 0x%x\n", |
| 267 | subs->sync_endpoint->ep_num); |
Takashi Iwai | b2eb950 | 2012-11-21 08:30:48 +0100 | [diff] [blame] | 268 | snd_usb_endpoint_stop(subs->sync_endpoint); |
Takashi Iwai | 1803503 | 2020-11-23 09:53:12 +0100 | [diff] [blame] | 269 | subs->sync_endpoint->sync_slave = NULL; |
| 270 | } |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 271 | |
Takashi Iwai | e93e890 | 2020-11-23 09:53:13 +0100 | [diff] [blame] | 272 | if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) { |
| 273 | dev_dbg(&subs->dev->dev, "Stopping data EP 0x%x\n", |
| 274 | subs->data_endpoint->ep_num); |
Takashi Iwai | b2eb950 | 2012-11-21 08:30:48 +0100 | [diff] [blame] | 275 | snd_usb_endpoint_stop(subs->data_endpoint); |
Takashi Iwai | e93e890 | 2020-11-23 09:53:13 +0100 | [diff] [blame] | 276 | } |
Takashi Iwai | dc5eafe | 2019-12-10 07:34:54 +0100 | [diff] [blame] | 277 | } |
Takashi Iwai | b2eb950 | 2012-11-21 08:30:48 +0100 | [diff] [blame] | 278 | |
Takashi Iwai | dc5eafe | 2019-12-10 07:34:54 +0100 | [diff] [blame] | 279 | /* PCM sync_stop callback */ |
| 280 | static int snd_usb_pcm_sync_stop(struct snd_pcm_substream *substream) |
| 281 | { |
| 282 | struct snd_usb_substream *subs = substream->runtime->private_data; |
| 283 | |
| 284 | if (!snd_usb_lock_shutdown(subs->stream->chip)) { |
| 285 | sync_pending_stops(subs); |
| 286 | snd_usb_unlock_shutdown(subs->stream->chip); |
Takashi Iwai | b2eb950 | 2012-11-21 08:30:48 +0100 | [diff] [blame] | 287 | } |
Takashi Iwai | dc5eafe | 2019-12-10 07:34:54 +0100 | [diff] [blame] | 288 | return 0; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 289 | } |
| 290 | |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 291 | /* Check whether the given iface:altsetting points to an implicit fb source */ |
Takashi Iwai | e42a09b | 2020-11-23 09:53:22 +0100 | [diff] [blame^] | 292 | static bool search_generic_implicit_fb(struct snd_usb_audio *chip, int ifnum, |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 293 | unsigned int altsetting, |
| 294 | struct usb_host_interface **altsp, |
| 295 | unsigned int *ep) |
Clemens Ladisch | ba7c2be | 2013-02-03 22:31:20 +0100 | [diff] [blame] | 296 | { |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 297 | struct usb_host_interface *alts; |
Clemens Ladisch | ba7c2be | 2013-02-03 22:31:20 +0100 | [diff] [blame] | 298 | struct usb_interface_descriptor *altsd; |
| 299 | struct usb_endpoint_descriptor *epd; |
| 300 | |
Takashi Iwai | e42a09b | 2020-11-23 09:53:22 +0100 | [diff] [blame^] | 301 | alts = snd_usb_get_host_interface(chip, ifnum, altsetting); |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 302 | if (!alts) |
| 303 | return false; |
| 304 | altsd = get_iface_desc(alts); |
| 305 | if (altsd->bInterfaceClass != USB_CLASS_AUDIO || |
| 306 | altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING || |
| 307 | altsd->bInterfaceProtocol != UAC_VERSION_2 || |
Clemens Ladisch | ba7c2be | 2013-02-03 22:31:20 +0100 | [diff] [blame] | 308 | altsd->bNumEndpoints < 1) |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 309 | return false; |
| 310 | epd = get_endpoint(alts, 0); |
Clemens Ladisch | ba7c2be | 2013-02-03 22:31:20 +0100 | [diff] [blame] | 311 | if (!usb_endpoint_is_isoc_in(epd) || |
| 312 | (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) != |
| 313 | USB_ENDPOINT_USAGE_IMPLICIT_FB) |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 314 | return false; |
Clemens Ladisch | ba7c2be | 2013-02-03 22:31:20 +0100 | [diff] [blame] | 315 | *ep = epd->bEndpointAddress; |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 316 | *altsp = alts; |
| 317 | return true; |
| 318 | } |
| 319 | |
| 320 | /* Like the function above, but specific to Roland with vendor class and hack */ |
Takashi Iwai | e42a09b | 2020-11-23 09:53:22 +0100 | [diff] [blame^] | 321 | static bool search_roland_implicit_fb(struct snd_usb_audio *chip, int ifnum, |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 322 | unsigned int altsetting, |
| 323 | struct usb_host_interface **altsp, |
| 324 | unsigned int *ep) |
| 325 | { |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 326 | struct usb_host_interface *alts; |
| 327 | struct usb_interface_descriptor *altsd; |
| 328 | struct usb_endpoint_descriptor *epd; |
| 329 | |
Takashi Iwai | e42a09b | 2020-11-23 09:53:22 +0100 | [diff] [blame^] | 330 | alts = snd_usb_get_host_interface(chip, ifnum, altsetting); |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 331 | if (!alts) |
| 332 | return false; |
| 333 | altsd = get_iface_desc(alts); |
| 334 | if (altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC || |
| 335 | (altsd->bInterfaceSubClass != 2 && |
| 336 | altsd->bInterfaceProtocol != 2) || |
| 337 | altsd->bNumEndpoints < 1) |
| 338 | return false; |
| 339 | epd = get_endpoint(alts, 0); |
| 340 | if (!usb_endpoint_is_isoc_in(epd) || |
| 341 | (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) != |
| 342 | USB_ENDPOINT_USAGE_IMPLICIT_FB) |
| 343 | return false; |
| 344 | *ep = epd->bEndpointAddress; |
| 345 | *altsp = alts; |
| 346 | return true; |
Clemens Ladisch | ba7c2be | 2013-02-03 22:31:20 +0100 | [diff] [blame] | 347 | } |
| 348 | |
Manuel Reinhardt | 2bc16b9 | 2019-01-31 15:32:35 +0100 | [diff] [blame] | 349 | /* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk |
| 350 | * applies. Returns 1 if a quirk was found. |
| 351 | */ |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 352 | static int audioformat_implicit_fb_quirk(struct snd_usb_audio *chip, |
| 353 | struct audioformat *fmt, |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 354 | struct usb_host_interface *alts) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 355 | { |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 356 | struct usb_device *dev = chip->dev; |
| 357 | struct usb_interface_descriptor *altsd = get_iface_desc(alts); |
Takashi Iwai | e42a09b | 2020-11-23 09:53:22 +0100 | [diff] [blame^] | 358 | struct usb_interface *iface; |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 359 | unsigned int attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE; |
Eldad Zack | a60945f | 2013-08-03 10:50:18 +0200 | [diff] [blame] | 360 | unsigned int ep; |
Alberto Aguirre | 103e962 | 2018-04-18 09:35:34 -0500 | [diff] [blame] | 361 | unsigned int ifnum; |
Daniel Mack | c75a8a7 | 2012-04-12 13:51:14 +0200 | [diff] [blame] | 362 | |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 363 | switch (chip->usb_id) { |
Eldad Zack | ca10a7e | 2012-11-28 23:55:41 +0100 | [diff] [blame] | 364 | case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */ |
Matt Gruskin | e9a25e0 | 2013-02-09 12:56:35 -0500 | [diff] [blame] | 365 | case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */ |
Geoffrey D. Bennett | 0938eca | 2020-11-04 22:27:17 +1030 | [diff] [blame] | 366 | case USB_ID(0x22f0, 0x0006): /* Allen&Heath Qu-16 */ |
Eldad Zack | 914273c | 2013-08-03 10:50:21 +0200 | [diff] [blame] | 367 | ep = 0x81; |
Alberto Aguirre | 103e962 | 2018-04-18 09:35:34 -0500 | [diff] [blame] | 368 | ifnum = 3; |
| 369 | goto add_sync_ep_from_ifnum; |
Daniel Mack | c75a8a7 | 2012-04-12 13:51:14 +0200 | [diff] [blame] | 370 | case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */ |
| 371 | case USB_ID(0x0763, 0x2081): |
Eldad Zack | 914273c | 2013-08-03 10:50:21 +0200 | [diff] [blame] | 372 | ep = 0x81; |
Alberto Aguirre | 103e962 | 2018-04-18 09:35:34 -0500 | [diff] [blame] | 373 | ifnum = 2; |
| 374 | goto add_sync_ep_from_ifnum; |
| 375 | case USB_ID(0x2466, 0x8003): /* Fractal Audio Axe-Fx II */ |
Geoffrey D. Bennett | 26201dd | 2020-11-04 22:37:05 +1030 | [diff] [blame] | 376 | case USB_ID(0x0499, 0x172a): /* Yamaha MODX */ |
Alberto Aguirre | 17f08b0 | 2016-12-08 00:36:48 -0600 | [diff] [blame] | 377 | ep = 0x86; |
Alberto Aguirre | 103e962 | 2018-04-18 09:35:34 -0500 | [diff] [blame] | 378 | ifnum = 2; |
| 379 | goto add_sync_ep_from_ifnum; |
Alberto Aguirre | 91a8561 | 2018-04-18 09:35:35 -0500 | [diff] [blame] | 380 | case USB_ID(0x2466, 0x8010): /* Fractal Audio Axe-Fx III */ |
| 381 | ep = 0x81; |
| 382 | ifnum = 2; |
| 383 | goto add_sync_ep_from_ifnum; |
Keith Winstein | f15cfca | 2020-10-25 22:05:47 -0700 | [diff] [blame] | 384 | case USB_ID(0x1686, 0xf029): /* Zoom UAC-2 */ |
| 385 | ep = 0x82; |
| 386 | ifnum = 2; |
| 387 | goto add_sync_ep_from_ifnum; |
Takashi Iwai | 1a15718 | 2019-08-20 08:58:12 +0200 | [diff] [blame] | 388 | case USB_ID(0x1397, 0x0001): /* Behringer UFX1604 */ |
Alberto Aguirre | 103e962 | 2018-04-18 09:35:34 -0500 | [diff] [blame] | 389 | case USB_ID(0x1397, 0x0002): /* Behringer UFX1204 */ |
Lassi Ylikojola | 5e35dc0 | 2018-02-09 16:51:36 +0200 | [diff] [blame] | 390 | ep = 0x81; |
Alberto Aguirre | 103e962 | 2018-04-18 09:35:34 -0500 | [diff] [blame] | 391 | ifnum = 1; |
| 392 | goto add_sync_ep_from_ifnum; |
Alexander Tsoy | 2edb84e | 2020-02-29 18:18:15 +0300 | [diff] [blame] | 393 | case USB_ID(0x07fd, 0x0004): /* MOTU MicroBook II/IIc */ |
| 394 | /* MicroBook IIc */ |
| 395 | if (altsd->bInterfaceClass == USB_CLASS_AUDIO) |
| 396 | return 0; |
| 397 | |
| 398 | /* MicroBook II */ |
Manuel Reinhardt | a634090 | 2019-02-28 20:34:04 +0100 | [diff] [blame] | 399 | ep = 0x84; |
| 400 | ifnum = 0; |
| 401 | goto add_sync_ep_from_ifnum; |
Alexander Tsoy | c249177 | 2020-01-15 18:13:58 +0300 | [diff] [blame] | 402 | case USB_ID(0x07fd, 0x0008): /* MOTU M Series */ |
Laurence Tratt | 3da87ec | 2020-06-21 08:50:05 +0100 | [diff] [blame] | 403 | case USB_ID(0x31e9, 0x0001): /* Solid State Logic SSL2 */ |
Laurence Tratt | e7585db | 2020-06-12 12:18:07 +0100 | [diff] [blame] | 404 | case USB_ID(0x31e9, 0x0002): /* Solid State Logic SSL2+ */ |
Joshua Sivec | 7c5b892 | 2020-08-25 18:55:18 +0200 | [diff] [blame] | 405 | case USB_ID(0x0499, 0x172f): /* Steinberg UR22C */ |
Pavel Hofman | b6a1e78 | 2020-07-03 12:04:33 +0200 | [diff] [blame] | 406 | case USB_ID(0x0d9a, 0x00df): /* RTX6001 */ |
Alexander Tsoy | c249177 | 2020-01-15 18:13:58 +0300 | [diff] [blame] | 407 | ep = 0x81; |
| 408 | ifnum = 2; |
| 409 | goto add_sync_ep_from_ifnum; |
Dmitry Panchenko | 7fccfec | 2020-06-01 13:22:24 +0300 | [diff] [blame] | 410 | case USB_ID(0x2b73, 0x000a): /* Pioneer DJ DJM-900NXS2 */ |
František Kučera | 14335d8 | 2020-08-25 17:31:13 +0200 | [diff] [blame] | 411 | case USB_ID(0x2b73, 0x0017): /* Pioneer DJ DJM-250MK2 */ |
Dmitry Panchenko | 7fccfec | 2020-06-01 13:22:24 +0300 | [diff] [blame] | 412 | ep = 0x82; |
| 413 | ifnum = 0; |
| 414 | goto add_sync_ep_from_ifnum; |
Szabolcs Szőke | 7571b6a | 2019-10-11 19:19:36 +0200 | [diff] [blame] | 415 | case USB_ID(0x0582, 0x01d8): /* BOSS Katana */ |
| 416 | /* BOSS Katana amplifiers do not need quirks */ |
| 417 | return 0; |
Daniel Mack | c75a8a7 | 2012-04-12 13:51:14 +0200 | [diff] [blame] | 418 | } |
Alberto Aguirre | 103e962 | 2018-04-18 09:35:34 -0500 | [diff] [blame] | 419 | |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 420 | /* Generic UAC2 implicit feedback */ |
| 421 | if (attr == USB_ENDPOINT_SYNC_ASYNC && |
| 422 | altsd->bInterfaceClass == USB_CLASS_AUDIO && |
| 423 | altsd->bInterfaceProtocol == UAC_VERSION_2 && |
| 424 | altsd->bNumEndpoints == 1) { |
| 425 | ifnum = altsd->bInterfaceNumber + 1; |
Takashi Iwai | e42a09b | 2020-11-23 09:53:22 +0100 | [diff] [blame^] | 426 | if (search_generic_implicit_fb(chip, ifnum, |
Takashi Iwai | 2e43aae | 2020-11-23 09:53:10 +0100 | [diff] [blame] | 427 | altsd->bAlternateSetting, |
| 428 | &alts, &ep)) |
| 429 | goto add_sync_ep; |
| 430 | } |
| 431 | |
| 432 | /* Roland/BOSS implicit feedback with vendor spec class */ |
Eldad Zack | 914273c | 2013-08-03 10:50:21 +0200 | [diff] [blame] | 433 | if (attr == USB_ENDPOINT_SYNC_ASYNC && |
Clemens Ladisch | ba7c2be | 2013-02-03 22:31:20 +0100 | [diff] [blame] | 434 | altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC && |
| 435 | altsd->bInterfaceProtocol == 2 && |
| 436 | altsd->bNumEndpoints == 1 && |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 437 | USB_ID_VENDOR(chip->usb_id) == 0x0582 /* Roland */) { |
| 438 | ifnum = altsd->bInterfaceNumber + 1; |
Takashi Iwai | e42a09b | 2020-11-23 09:53:22 +0100 | [diff] [blame^] | 439 | if (search_roland_implicit_fb(chip, ifnum, |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 440 | altsd->bAlternateSetting, |
| 441 | &alts, &ep)) |
| 442 | goto add_sync_ep; |
| 443 | } |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 444 | |
Eldad Zack | a60945f | 2013-08-03 10:50:18 +0200 | [diff] [blame] | 445 | /* No quirk */ |
| 446 | return 0; |
| 447 | |
Alberto Aguirre | 103e962 | 2018-04-18 09:35:34 -0500 | [diff] [blame] | 448 | add_sync_ep_from_ifnum: |
| 449 | iface = usb_ifnum_to_if(dev, ifnum); |
| 450 | |
Johan Hovold | 5d1b712 | 2020-01-14 09:39:53 +0100 | [diff] [blame] | 451 | if (!iface || iface->num_altsetting < 2) |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 452 | return 0; |
Alberto Aguirre | 103e962 | 2018-04-18 09:35:34 -0500 | [diff] [blame] | 453 | |
| 454 | alts = &iface->altsetting[1]; |
| 455 | |
Eldad Zack | a60945f | 2013-08-03 10:50:18 +0200 | [diff] [blame] | 456 | add_sync_ep: |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 457 | fmt->sync_ep = ep; |
| 458 | fmt->sync_iface = ifnum; |
| 459 | fmt->sync_altsetting = alts->desc.bAlternateSetting; |
| 460 | fmt->implicit_fb = 1; |
| 461 | dev_dbg(&dev->dev, "%d:%d: found implicit_fb sync_ep=%x, iface=%d, alt=%d\n", |
| 462 | fmt->iface, fmt->altsetting, fmt->sync_ep, fmt->sync_iface, |
| 463 | fmt->sync_altsetting); |
Eldad Zack | a60945f | 2013-08-03 10:50:18 +0200 | [diff] [blame] | 464 | |
Manuel Reinhardt | 2bc16b9 | 2019-01-31 15:32:35 +0100 | [diff] [blame] | 465 | return 1; |
Eldad Zack | a60945f | 2013-08-03 10:50:18 +0200 | [diff] [blame] | 466 | } |
| 467 | |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 468 | int snd_usb_audioformat_set_sync_ep(struct snd_usb_audio *chip, |
| 469 | struct audioformat *fmt) |
Eldad Zack | a60945f | 2013-08-03 10:50:18 +0200 | [diff] [blame] | 470 | { |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 471 | struct usb_device *dev = chip->dev; |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 472 | struct usb_host_interface *alts; |
| 473 | struct usb_interface_descriptor *altsd; |
| 474 | unsigned int ep, attr, sync_attr; |
| 475 | bool is_playback; |
Eldad Zack | a60945f | 2013-08-03 10:50:18 +0200 | [diff] [blame] | 476 | int err; |
| 477 | |
Takashi Iwai | e42a09b | 2020-11-23 09:53:22 +0100 | [diff] [blame^] | 478 | alts = snd_usb_get_host_interface(chip, fmt->iface, fmt->altsetting); |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 479 | if (!alts) |
| 480 | return 0; |
| 481 | altsd = get_iface_desc(alts); |
| 482 | |
| 483 | is_playback = !(get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN); |
| 484 | if (is_playback) { |
Takashi Iwai | e42a09b | 2020-11-23 09:53:22 +0100 | [diff] [blame^] | 485 | err = audioformat_implicit_fb_quirk(chip, fmt, alts); |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 486 | if (err > 0) |
| 487 | return 0; |
| 488 | } |
Manuel Reinhardt | 2bc16b9 | 2019-01-31 15:32:35 +0100 | [diff] [blame] | 489 | |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 490 | if (altsd->bNumEndpoints < 2) |
| 491 | return 0; |
Daniel Mack | c75a8a7 | 2012-04-12 13:51:14 +0200 | [diff] [blame] | 492 | |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 493 | attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE; |
Pierre-Louis Bossart | 395ae54 | 2015-08-14 17:19:43 -0500 | [diff] [blame] | 494 | if ((is_playback && (attr == USB_ENDPOINT_SYNC_SYNC || |
| 495 | attr == USB_ENDPOINT_SYNC_ADAPTIVE)) || |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 496 | (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE)) |
| 497 | return 0; |
Daniel Mack | c75a8a7 | 2012-04-12 13:51:14 +0200 | [diff] [blame] | 498 | |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 499 | sync_attr = get_endpoint(alts, 1)->bmAttributes; |
| 500 | |
Pierre-Louis Bossart | 395ae54 | 2015-08-14 17:19:43 -0500 | [diff] [blame] | 501 | /* |
| 502 | * In case of illegal SYNC_NONE for OUT endpoint, we keep going to see |
| 503 | * if we don't find a sync endpoint, as on M-Audio Transit. In case of |
| 504 | * error fall back to SYNC mode and don't create sync endpoint |
| 505 | */ |
| 506 | |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 507 | /* check sync-pipe endpoint */ |
| 508 | /* ... and check descriptor size before accessing bSynchAddress |
| 509 | because there is a version of the SB Audigy 2 NX firmware lacking |
| 510 | the audio fields in the endpoint descriptors */ |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 511 | if ((sync_attr & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC || |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 512 | (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && |
Eldad Zack | 95fec88 | 2013-08-03 10:50:20 +0200 | [diff] [blame] | 513 | get_endpoint(alts, 1)->bSynchAddress != 0)) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 514 | dev_err(&dev->dev, |
| 515 | "%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n", |
| 516 | fmt->iface, fmt->altsetting, |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 517 | get_endpoint(alts, 1)->bmAttributes, |
| 518 | get_endpoint(alts, 1)->bLength, |
| 519 | get_endpoint(alts, 1)->bSynchAddress); |
Pierre-Louis Bossart | 395ae54 | 2015-08-14 17:19:43 -0500 | [diff] [blame] | 520 | if (is_playback && attr == USB_ENDPOINT_SYNC_NONE) |
| 521 | return 0; |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 522 | return -EINVAL; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 523 | } |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 524 | ep = get_endpoint(alts, 1)->bEndpointAddress; |
Eldad Zack | 95fec88 | 2013-08-03 10:50:20 +0200 | [diff] [blame] | 525 | if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && |
Ard van Breemen | 1b34121 | 2019-08-02 13:52:14 +0200 | [diff] [blame] | 526 | get_endpoint(alts, 0)->bSynchAddress != 0 && |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 527 | ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) || |
| 528 | (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 529 | dev_err(&dev->dev, |
| 530 | "%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n", |
| 531 | fmt->iface, fmt->altsetting, |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 532 | is_playback, ep, get_endpoint(alts, 0)->bSynchAddress); |
Pierre-Louis Bossart | 395ae54 | 2015-08-14 17:19:43 -0500 | [diff] [blame] | 533 | if (is_playback && attr == USB_ENDPOINT_SYNC_NONE) |
| 534 | return 0; |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 535 | return -EINVAL; |
| 536 | } |
| 537 | |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 538 | fmt->sync_ep = ep; |
| 539 | fmt->sync_iface = altsd->bInterfaceNumber; |
| 540 | fmt->sync_altsetting = altsd->bAlternateSetting; |
| 541 | if ((sync_attr & USB_ENDPOINT_USAGE_MASK) == USB_ENDPOINT_USAGE_IMPLICIT_FB) |
| 542 | fmt->implicit_fb = 1; |
| 543 | |
| 544 | dev_dbg(&dev->dev, "%d:%d: found sync_ep=0x%x, iface=%d, alt=%d, implicit_fb=%d\n", |
| 545 | fmt->iface, fmt->altsetting, fmt->sync_ep, fmt->sync_iface, |
| 546 | fmt->sync_altsetting, fmt->implicit_fb); |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | static int set_sync_endpoint(struct snd_usb_substream *subs, |
| 552 | struct audioformat *fmt) |
| 553 | { |
| 554 | struct usb_device *dev = subs->dev; |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 555 | struct usb_host_interface *alts; |
| 556 | int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK; |
| 557 | unsigned int ep; |
| 558 | int err; |
| 559 | |
| 560 | subs->sync_endpoint = NULL; |
| 561 | subs->data_endpoint->sync_master = NULL; |
| 562 | |
| 563 | ep = fmt->sync_ep; |
| 564 | if (!ep) |
| 565 | return 0; |
| 566 | |
Takashi Iwai | e42a09b | 2020-11-23 09:53:22 +0100 | [diff] [blame^] | 567 | alts = snd_usb_get_host_interface(subs->stream->chip, fmt->sync_iface, |
| 568 | fmt->altsetting); |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 569 | if (!alts) |
| 570 | return 0; |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 571 | |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 572 | subs->sync_endpoint = snd_usb_get_endpoint(subs->stream->chip, ep); |
Pierre-Louis Bossart | 395ae54 | 2015-08-14 17:19:43 -0500 | [diff] [blame] | 573 | if (!subs->sync_endpoint) { |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 574 | if (is_playback && |
| 575 | (fmt->ep_attr & USB_ENDPOINT_SYNCTYPE) == USB_ENDPOINT_SYNC_NONE) |
Pierre-Louis Bossart | 395ae54 | 2015-08-14 17:19:43 -0500 | [diff] [blame] | 576 | return 0; |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 577 | return -EINVAL; |
Pierre-Louis Bossart | 395ae54 | 2015-08-14 17:19:43 -0500 | [diff] [blame] | 578 | } |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 579 | |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 580 | subs->sync_endpoint->iface = fmt->sync_iface; |
| 581 | subs->sync_endpoint->altsetting = fmt->sync_altsetting; |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 582 | subs->sync_endpoint->is_implicit_feedback = fmt->implicit_fb; |
Erwin Burema | 10ce77e | 2020-05-10 20:29:11 +0200 | [diff] [blame] | 583 | |
Eldad Zack | f34d065 | 2013-08-03 10:50:19 +0200 | [diff] [blame] | 584 | subs->data_endpoint->sync_master = subs->sync_endpoint; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 585 | |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 586 | snd_usb_endpoint_set_syncinterval(subs->stream->chip, subs->sync_endpoint, alts); |
| 587 | |
Takashi Iwai | 5fd255f | 2020-11-23 09:53:19 +0100 | [diff] [blame] | 588 | if (!subs->sync_endpoint->use_count && |
| 589 | (subs->data_endpoint->iface != subs->sync_endpoint->iface || |
| 590 | subs->data_endpoint->altsetting != subs->sync_endpoint->altsetting)) { |
Takashi Iwai | 4974b79 | 2020-11-23 09:53:08 +0100 | [diff] [blame] | 591 | err = usb_set_interface(subs->dev, |
| 592 | subs->sync_endpoint->iface, |
| 593 | subs->sync_endpoint->altsetting); |
| 594 | if (err < 0) |
| 595 | return err; |
| 596 | dev_dbg(&dev->dev, "setting usb interface %d:%d\n", |
| 597 | subs->sync_endpoint->iface, |
| 598 | subs->sync_endpoint->altsetting); |
| 599 | snd_usb_set_interface_quirk(dev); |
| 600 | } |
| 601 | |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | /* |
| 606 | * find a matching format and set up the interface |
| 607 | */ |
| 608 | static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt) |
| 609 | { |
| 610 | struct usb_device *dev = subs->dev; |
| 611 | struct usb_host_interface *alts; |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 612 | struct usb_interface *iface; |
Takashi Iwai | 5fd255f | 2020-11-23 09:53:19 +0100 | [diff] [blame] | 613 | struct snd_usb_endpoint *ep; |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 614 | int err; |
| 615 | |
| 616 | iface = usb_ifnum_to_if(dev, fmt->iface); |
| 617 | if (WARN_ON(!iface)) |
| 618 | return -EINVAL; |
Takashi Iwai | b099b96 | 2018-05-02 09:36:28 +0200 | [diff] [blame] | 619 | alts = usb_altnum_to_altsetting(iface, fmt->altsetting); |
Johan Hovold | 0141254 | 2019-12-20 10:31:34 +0100 | [diff] [blame] | 620 | if (WARN_ON(!alts)) |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 621 | return -EINVAL; |
| 622 | |
Hui Wang | 92adc96 | 2019-12-18 21:26:50 +0800 | [diff] [blame] | 623 | if (fmt == subs->cur_audiofmt && !subs->need_setup_fmt) |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 624 | return 0; |
| 625 | |
Takashi Iwai | 5fd255f | 2020-11-23 09:53:19 +0100 | [diff] [blame] | 626 | /* shared EP with implicit fb */ |
| 627 | if (fmt->implicit_fb && !subs->need_setup_fmt) { |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 628 | ep = snd_usb_get_endpoint(subs->stream->chip, fmt->endpoint); |
Takashi Iwai | 5fd255f | 2020-11-23 09:53:19 +0100 | [diff] [blame] | 629 | if (ep && ep->use_count > 0) |
| 630 | goto add_data_ep; |
| 631 | } |
| 632 | |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 633 | /* close the old interface */ |
Hui Wang | 92adc96 | 2019-12-18 21:26:50 +0800 | [diff] [blame] | 634 | if (subs->interface >= 0 && (subs->interface != fmt->iface || subs->need_setup_fmt)) { |
Takashi Iwai | 9821505 | 2020-11-23 09:53:21 +0100 | [diff] [blame] | 635 | err = usb_set_interface(subs->dev, subs->interface, 0); |
| 636 | if (err < 0) { |
| 637 | dev_err(&dev->dev, |
| 638 | "%d:%d: return to setting 0 failed (%d)\n", |
| 639 | fmt->iface, fmt->altsetting, err); |
| 640 | return -EIO; |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 641 | } |
| 642 | subs->interface = -1; |
| 643 | subs->altset_idx = 0; |
| 644 | } |
| 645 | |
Hui Wang | 92adc96 | 2019-12-18 21:26:50 +0800 | [diff] [blame] | 646 | if (subs->need_setup_fmt) |
| 647 | subs->need_setup_fmt = false; |
| 648 | |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 649 | /* set interface */ |
Takashi Iwai | b099b96 | 2018-05-02 09:36:28 +0200 | [diff] [blame] | 650 | if (iface->cur_altsetting != alts) { |
Jurgen Kramer | 6874daa | 2014-11-28 17:32:54 +0100 | [diff] [blame] | 651 | err = snd_usb_select_mode_quirk(subs, fmt); |
| 652 | if (err < 0) |
| 653 | return -EIO; |
| 654 | |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 655 | err = usb_set_interface(dev, fmt->iface, fmt->altsetting); |
| 656 | if (err < 0) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 657 | dev_err(&dev->dev, |
| 658 | "%d:%d: usb_set_interface failed (%d)\n", |
| 659 | fmt->iface, fmt->altsetting, err); |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 660 | return -EIO; |
| 661 | } |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 662 | dev_dbg(&dev->dev, "setting usb interface %d:%d\n", |
| 663 | fmt->iface, fmt->altsetting); |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 664 | snd_usb_set_interface_quirk(dev); |
| 665 | } |
| 666 | |
Takashi Iwai | 5fd255f | 2020-11-23 09:53:19 +0100 | [diff] [blame] | 667 | subs->need_setup_ep = true; |
| 668 | |
| 669 | add_data_ep: |
Takashi Iwai | b099b96 | 2018-05-02 09:36:28 +0200 | [diff] [blame] | 670 | subs->interface = fmt->iface; |
| 671 | subs->altset_idx = fmt->altset_idx; |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 672 | subs->data_endpoint = snd_usb_get_endpoint(subs->stream->chip, |
| 673 | fmt->endpoint); |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 674 | if (!subs->data_endpoint) |
| 675 | return -EINVAL; |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 676 | subs->data_endpoint->iface = fmt->iface; |
| 677 | subs->data_endpoint->altsetting = fmt->altsetting; |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 678 | |
Takashi Iwai | f6581c0 | 2020-11-23 09:53:14 +0100 | [diff] [blame] | 679 | err = set_sync_endpoint(subs, fmt); |
Eldad Zack | 71bb64c | 2013-08-03 10:50:17 +0200 | [diff] [blame] | 680 | if (err < 0) |
| 681 | return err; |
| 682 | |
Takashi Iwai | 5fd255f | 2020-11-23 09:53:19 +0100 | [diff] [blame] | 683 | if (subs->need_setup_ep) { |
| 684 | err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt); |
| 685 | if (err < 0) |
| 686 | return err; |
| 687 | } |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 688 | |
| 689 | subs->cur_audiofmt = fmt; |
| 690 | |
| 691 | snd_usb_set_format_quirk(subs, fmt); |
| 692 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | /* |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 697 | * Return the score of matching two audioformats. |
| 698 | * Veto the audioformat if: |
| 699 | * - It has no channels for some reason. |
| 700 | * - Requested PCM format is not supported. |
| 701 | * - Requested sample rate is not supported. |
| 702 | */ |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 703 | static int match_endpoint_audioformats(struct snd_usb_substream *subs, |
| 704 | struct audioformat *fp, |
| 705 | struct audioformat *match, int rate, |
| 706 | snd_pcm_format_t pcm_format) |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 707 | { |
| 708 | int i; |
| 709 | int score = 0; |
| 710 | |
| 711 | if (fp->channels < 1) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 712 | dev_dbg(&subs->dev->dev, |
| 713 | "%s: (fmt @%p) no channels\n", __func__, fp); |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 714 | return 0; |
| 715 | } |
| 716 | |
Eldad Zack | 74c34ca | 2013-04-23 01:00:41 +0200 | [diff] [blame] | 717 | if (!(fp->formats & pcm_format_to_bits(pcm_format))) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 718 | dev_dbg(&subs->dev->dev, |
| 719 | "%s: (fmt @%p) no match for format %d\n", __func__, |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 720 | fp, pcm_format); |
| 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | for (i = 0; i < fp->nr_rates; i++) { |
| 725 | if (fp->rate_table[i] == rate) { |
| 726 | score++; |
| 727 | break; |
| 728 | } |
| 729 | } |
| 730 | if (!score) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 731 | dev_dbg(&subs->dev->dev, |
| 732 | "%s: (fmt @%p) no match for rate %d\n", __func__, |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 733 | fp, rate); |
| 734 | return 0; |
| 735 | } |
| 736 | |
| 737 | if (fp->channels == match->channels) |
| 738 | score++; |
| 739 | |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 740 | dev_dbg(&subs->dev->dev, |
| 741 | "%s: (fmt @%p) score %d\n", __func__, fp, score); |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 742 | |
| 743 | return score; |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | * Configure the sync ep using the rate and pcm format of the data ep. |
| 748 | */ |
| 749 | static int configure_sync_endpoint(struct snd_usb_substream *subs) |
| 750 | { |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 751 | struct audioformat *fp; |
| 752 | struct audioformat *sync_fp = NULL; |
| 753 | int cur_score = 0; |
| 754 | int sync_period_bytes = subs->period_bytes; |
| 755 | struct snd_usb_substream *sync_subs = |
| 756 | &subs->stream->substream[subs->direction ^ 1]; |
| 757 | |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 758 | if (subs->fixed_hw || |
| 759 | !subs->sync_endpoint->is_implicit_feedback) { |
| 760 | sync_fp = subs->cur_audiofmt; |
| 761 | goto configure; |
| 762 | } |
| 763 | |
| 764 | sync_fp = find_format(&sync_subs->fmt_list, subs->pcm_format, |
| 765 | subs->cur_rate, subs->channels, NULL); |
| 766 | if (sync_fp) |
| 767 | goto configure; |
Takashi Iwai | 31be542 | 2013-01-10 14:06:38 +0100 | [diff] [blame] | 768 | |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 769 | /* Try to find the best matching audioformat. */ |
| 770 | list_for_each_entry(fp, &sync_subs->fmt_list, list) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 771 | int score = match_endpoint_audioformats(subs, |
| 772 | fp, subs->cur_audiofmt, |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 773 | subs->cur_rate, subs->pcm_format); |
| 774 | |
| 775 | if (score > cur_score) { |
| 776 | sync_fp = fp; |
| 777 | cur_score = score; |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | if (unlikely(sync_fp == NULL)) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 782 | dev_err(&subs->dev->dev, |
| 783 | "%s: no valid audioformat for sync ep %x found\n", |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 784 | __func__, sync_subs->ep_num); |
| 785 | return -EINVAL; |
| 786 | } |
| 787 | |
| 788 | /* |
| 789 | * Recalculate the period bytes if channel number differ between |
| 790 | * data and sync ep audioformat. |
| 791 | */ |
| 792 | if (sync_fp->channels != subs->channels) { |
| 793 | sync_period_bytes = (subs->period_bytes / subs->channels) * |
| 794 | sync_fp->channels; |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 795 | dev_dbg(&subs->dev->dev, |
| 796 | "%s: adjusted sync ep period bytes (%d -> %d)\n", |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 797 | __func__, subs->period_bytes, sync_period_bytes); |
| 798 | } |
| 799 | |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 800 | configure: |
| 801 | return snd_usb_endpoint_set_params(subs->sync_endpoint, |
| 802 | subs->pcm_format, |
| 803 | sync_fp->channels, |
| 804 | sync_period_bytes, |
| 805 | subs->period_frames, |
| 806 | subs->buffer_periods, |
| 807 | subs->cur_rate, |
| 808 | sync_fp, |
| 809 | NULL); |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | /* |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 813 | * configure endpoint params |
| 814 | * |
| 815 | * called during initial setup and upon resume |
| 816 | */ |
| 817 | static int configure_endpoint(struct snd_usb_substream *subs) |
| 818 | { |
| 819 | int ret; |
| 820 | |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 821 | /* format changed */ |
Takashi Iwai | dc5eafe | 2019-12-10 07:34:54 +0100 | [diff] [blame] | 822 | stop_endpoints(subs); |
| 823 | sync_pending_stops(subs); |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 824 | ret = snd_usb_endpoint_set_params(subs->data_endpoint, |
| 825 | subs->pcm_format, |
| 826 | subs->channels, |
| 827 | subs->period_bytes, |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 828 | subs->period_frames, |
| 829 | subs->buffer_periods, |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 830 | subs->cur_rate, |
| 831 | subs->cur_audiofmt, |
| 832 | subs->sync_endpoint); |
| 833 | if (ret < 0) |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 834 | return ret; |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 835 | |
| 836 | if (subs->sync_endpoint) |
Eldad Zack | 0d9741c | 2012-12-03 20:30:09 +0100 | [diff] [blame] | 837 | ret = configure_sync_endpoint(subs); |
| 838 | |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 839 | return ret; |
| 840 | } |
| 841 | |
Jorge Sanjuan | 3f59aa1 | 2018-07-31 13:28:44 +0100 | [diff] [blame] | 842 | static int snd_usb_pcm_change_state(struct snd_usb_substream *subs, int state) |
| 843 | { |
| 844 | int ret; |
| 845 | |
| 846 | if (!subs->str_pd) |
| 847 | return 0; |
| 848 | |
| 849 | ret = snd_usb_power_domain_set(subs->stream->chip, subs->str_pd, state); |
| 850 | if (ret < 0) { |
| 851 | dev_err(&subs->dev->dev, |
| 852 | "Cannot change Power Domain ID: %d to state: %d. Err: %d\n", |
| 853 | subs->str_pd->pd_id, state, ret); |
| 854 | return ret; |
| 855 | } |
| 856 | |
| 857 | return 0; |
| 858 | } |
| 859 | |
| 860 | int snd_usb_pcm_suspend(struct snd_usb_stream *as) |
| 861 | { |
| 862 | int ret; |
| 863 | |
| 864 | ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D2); |
| 865 | if (ret < 0) |
| 866 | return ret; |
| 867 | |
| 868 | ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D2); |
| 869 | if (ret < 0) |
| 870 | return ret; |
| 871 | |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | int snd_usb_pcm_resume(struct snd_usb_stream *as) |
| 876 | { |
| 877 | int ret; |
| 878 | |
Jorge Sanjuan | a0a4959 | 2018-07-31 13:28:45 +0100 | [diff] [blame] | 879 | ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D1); |
Jorge Sanjuan | 3f59aa1 | 2018-07-31 13:28:44 +0100 | [diff] [blame] | 880 | if (ret < 0) |
| 881 | return ret; |
| 882 | |
Jorge Sanjuan | a0a4959 | 2018-07-31 13:28:45 +0100 | [diff] [blame] | 883 | ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D1); |
Jorge Sanjuan | 3f59aa1 | 2018-07-31 13:28:44 +0100 | [diff] [blame] | 884 | if (ret < 0) |
| 885 | return ret; |
| 886 | |
| 887 | return 0; |
| 888 | } |
| 889 | |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 890 | /* |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 891 | * hw_params callback |
| 892 | * |
| 893 | * allocate a buffer and set the given audio format. |
| 894 | * |
| 895 | * so far we use a physically linear buffer although packetize transfer |
| 896 | * doesn't need a continuous area. |
| 897 | * if sg buffer is supported on the later version of alsa, we'll follow |
| 898 | * that. |
| 899 | */ |
| 900 | static int snd_usb_hw_params(struct snd_pcm_substream *substream, |
| 901 | struct snd_pcm_hw_params *hw_params) |
| 902 | { |
| 903 | struct snd_usb_substream *subs = substream->runtime->private_data; |
| 904 | struct audioformat *fmt; |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 905 | int ret; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 906 | |
Shuah Khan | 66354f1 | 2019-04-01 20:40:22 -0400 | [diff] [blame] | 907 | ret = snd_media_start_pipeline(subs); |
| 908 | if (ret) |
| 909 | return ret; |
| 910 | |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 911 | subs->pcm_format = params_format(hw_params); |
| 912 | subs->period_bytes = params_period_bytes(hw_params); |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 913 | subs->period_frames = params_period_size(hw_params); |
| 914 | subs->buffer_periods = params_periods(hw_params); |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 915 | subs->channels = params_channels(hw_params); |
| 916 | subs->cur_rate = params_rate(hw_params); |
| 917 | |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 918 | fmt = find_substream_format(subs); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 919 | if (!fmt) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 920 | dev_dbg(&subs->dev->dev, |
| 921 | "cannot set format: format = %#x, rate = %d, channels = %d\n", |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 922 | subs->pcm_format, subs->cur_rate, subs->channels); |
Shuah Khan | 66354f1 | 2019-04-01 20:40:22 -0400 | [diff] [blame] | 923 | ret = -EINVAL; |
| 924 | goto stop_pipeline; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 925 | } |
| 926 | |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 927 | ret = snd_usb_lock_shutdown(subs->stream->chip); |
| 928 | if (ret < 0) |
Shuah Khan | 66354f1 | 2019-04-01 20:40:22 -0400 | [diff] [blame] | 929 | goto stop_pipeline; |
Jorge Sanjuan | a0a4959 | 2018-07-31 13:28:45 +0100 | [diff] [blame] | 930 | |
| 931 | ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0); |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 932 | if (ret < 0) |
Jorge Sanjuan | a0a4959 | 2018-07-31 13:28:45 +0100 | [diff] [blame] | 933 | goto unlock; |
| 934 | |
| 935 | ret = set_format(subs, fmt); |
| 936 | if (ret < 0) |
| 937 | goto unlock; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 938 | |
Jorge Sanjuan | a0a4959 | 2018-07-31 13:28:45 +0100 | [diff] [blame] | 939 | unlock: |
| 940 | snd_usb_unlock_shutdown(subs->stream->chip); |
Shuah Khan | 66354f1 | 2019-04-01 20:40:22 -0400 | [diff] [blame] | 941 | if (ret < 0) |
| 942 | goto stop_pipeline; |
| 943 | return ret; |
| 944 | |
| 945 | stop_pipeline: |
| 946 | snd_media_stop_pipeline(subs); |
Jorge Sanjuan | a0a4959 | 2018-07-31 13:28:45 +0100 | [diff] [blame] | 947 | return ret; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | /* |
| 951 | * hw_free callback |
| 952 | * |
| 953 | * reset the audio format and release the buffer |
| 954 | */ |
| 955 | static int snd_usb_hw_free(struct snd_pcm_substream *substream) |
| 956 | { |
| 957 | struct snd_usb_substream *subs = substream->runtime->private_data; |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 958 | struct snd_usb_audio *chip = subs->stream->chip; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 959 | |
Shuah Khan | 66354f1 | 2019-04-01 20:40:22 -0400 | [diff] [blame] | 960 | snd_media_stop_pipeline(subs); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 961 | subs->cur_audiofmt = NULL; |
| 962 | subs->cur_rate = 0; |
| 963 | subs->period_bytes = 0; |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 964 | if (!snd_usb_lock_shutdown(chip)) { |
Takashi Iwai | dc5eafe | 2019-12-10 07:34:54 +0100 | [diff] [blame] | 965 | stop_endpoints(subs); |
| 966 | sync_pending_stops(subs); |
Eldad Zack | 26de5d0 | 2013-10-06 22:31:07 +0200 | [diff] [blame] | 967 | snd_usb_endpoint_deactivate(subs->sync_endpoint); |
| 968 | snd_usb_endpoint_deactivate(subs->data_endpoint); |
Takashi Iwai | 1803503 | 2020-11-23 09:53:12 +0100 | [diff] [blame] | 969 | if (subs->data_endpoint) { |
| 970 | subs->data_endpoint->sync_master = NULL; |
| 971 | subs->data_endpoint = NULL; |
| 972 | } |
| 973 | subs->sync_endpoint = NULL; |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 974 | snd_usb_unlock_shutdown(chip); |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 975 | } |
Takashi Iwai | f274baa | 2018-05-27 13:01:17 +0200 | [diff] [blame] | 976 | |
Takashi Iwai | 6dd9486ca | 2019-12-09 10:49:42 +0100 | [diff] [blame] | 977 | return 0; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | /* |
| 981 | * prepare callback |
| 982 | * |
| 983 | * only a few subtle things... |
| 984 | */ |
| 985 | static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream) |
| 986 | { |
| 987 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 988 | struct snd_usb_substream *subs = runtime->private_data; |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 989 | struct usb_host_interface *alts; |
| 990 | struct usb_interface *iface; |
| 991 | int ret; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 992 | |
| 993 | if (! subs->cur_audiofmt) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 994 | dev_err(&subs->dev->dev, "no format is specified!\n"); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 995 | return -ENXIO; |
| 996 | } |
| 997 | |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 998 | ret = snd_usb_lock_shutdown(subs->stream->chip); |
| 999 | if (ret < 0) |
| 1000 | return ret; |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 1001 | if (snd_BUG_ON(!subs->data_endpoint)) { |
| 1002 | ret = -EIO; |
| 1003 | goto unlock; |
| 1004 | } |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1005 | |
Jorge Sanjuan | a0a4959 | 2018-07-31 13:28:45 +0100 | [diff] [blame] | 1006 | ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0); |
| 1007 | if (ret < 0) |
| 1008 | goto unlock; |
| 1009 | |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 1010 | ret = set_format(subs, subs->cur_audiofmt); |
| 1011 | if (ret < 0) |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 1012 | goto unlock; |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 1013 | |
Takashi Iwai | 384dc085 | 2012-09-18 14:49:31 +0200 | [diff] [blame] | 1014 | if (subs->need_setup_ep) { |
Daniel Girnus | 1e2e3fe | 2016-12-06 14:46:15 +0900 | [diff] [blame] | 1015 | |
| 1016 | iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface); |
| 1017 | alts = &iface->altsetting[subs->cur_audiofmt->altset_idx]; |
| 1018 | ret = snd_usb_init_sample_rate(subs->stream->chip, |
| 1019 | subs->cur_audiofmt->iface, |
| 1020 | alts, |
| 1021 | subs->cur_audiofmt, |
| 1022 | subs->cur_rate); |
| 1023 | if (ret < 0) |
| 1024 | goto unlock; |
| 1025 | |
Takashi Iwai | 384dc085 | 2012-09-18 14:49:31 +0200 | [diff] [blame] | 1026 | ret = configure_endpoint(subs); |
| 1027 | if (ret < 0) |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 1028 | goto unlock; |
Takashi Iwai | 384dc085 | 2012-09-18 14:49:31 +0200 | [diff] [blame] | 1029 | subs->need_setup_ep = false; |
| 1030 | } |
Dylan Reid | 61a7095 | 2012-09-18 09:49:48 -0700 | [diff] [blame] | 1031 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1032 | /* some unit conversions in runtime */ |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1033 | subs->data_endpoint->maxframesize = |
| 1034 | bytes_to_frames(runtime, subs->data_endpoint->maxpacksize); |
| 1035 | subs->data_endpoint->curframesize = |
| 1036 | bytes_to_frames(runtime, subs->data_endpoint->curpacksize); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1037 | |
| 1038 | /* reset the pointer */ |
| 1039 | subs->hwptr_done = 0; |
| 1040 | subs->transfer_done = 0; |
Pierre-Louis Bossart | 294c4fb | 2011-09-06 19:15:34 -0500 | [diff] [blame] | 1041 | subs->last_delay = 0; |
| 1042 | subs->last_frame_number = 0; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1043 | runtime->delay = 0; |
| 1044 | |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1045 | /* for playback, submit the URBs now; otherwise, the first hwptr_done |
| 1046 | * updates for all URBs would happen at the same time when starting */ |
| 1047 | if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) |
Ioan-Adrian Ratiu | 1d0f953 | 2017-01-05 00:37:46 +0200 | [diff] [blame] | 1048 | ret = start_endpoints(subs); |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1049 | |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 1050 | unlock: |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 1051 | snd_usb_unlock_shutdown(subs->stream->chip); |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 1052 | return ret; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1053 | } |
| 1054 | |
Takashi Iwai | 7ec827b | 2020-11-23 09:53:18 +0100 | [diff] [blame] | 1055 | /* |
| 1056 | * h/w constraints |
| 1057 | */ |
| 1058 | |
| 1059 | #ifdef HW_CONST_DEBUG |
| 1060 | #define hwc_debug(fmt, args...) pr_debug(fmt, ##args) |
| 1061 | #else |
| 1062 | #define hwc_debug(fmt, args...) do { } while(0) |
| 1063 | #endif |
| 1064 | |
Bhumika Goyal | aaffbf7 | 2017-08-17 14:45:59 +0530 | [diff] [blame] | 1065 | static const struct snd_pcm_hardware snd_usb_hardware = |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1066 | { |
| 1067 | .info = SNDRV_PCM_INFO_MMAP | |
| 1068 | SNDRV_PCM_INFO_MMAP_VALID | |
| 1069 | SNDRV_PCM_INFO_BATCH | |
| 1070 | SNDRV_PCM_INFO_INTERLEAVED | |
| 1071 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 1072 | SNDRV_PCM_INFO_PAUSE, |
| 1073 | .buffer_bytes_max = 1024 * 1024, |
| 1074 | .period_bytes_min = 64, |
| 1075 | .period_bytes_max = 512 * 1024, |
| 1076 | .periods_min = 2, |
| 1077 | .periods_max = 1024, |
| 1078 | }; |
| 1079 | |
| 1080 | static int hw_check_valid_format(struct snd_usb_substream *subs, |
| 1081 | struct snd_pcm_hw_params *params, |
| 1082 | struct audioformat *fp) |
| 1083 | { |
| 1084 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); |
| 1085 | struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); |
| 1086 | struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); |
| 1087 | struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME); |
Clemens Ladisch | 015eb0b | 2010-03-04 19:46:15 +0100 | [diff] [blame] | 1088 | struct snd_mask check_fmts; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1089 | unsigned int ptime; |
| 1090 | |
| 1091 | /* check the format */ |
Clemens Ladisch | 015eb0b | 2010-03-04 19:46:15 +0100 | [diff] [blame] | 1092 | snd_mask_none(&check_fmts); |
| 1093 | check_fmts.bits[0] = (u32)fp->formats; |
| 1094 | check_fmts.bits[1] = (u32)(fp->formats >> 32); |
| 1095 | snd_mask_intersect(&check_fmts, fmts); |
| 1096 | if (snd_mask_empty(&check_fmts)) { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1097 | hwc_debug(" > check: no supported format %d\n", fp->format); |
| 1098 | return 0; |
| 1099 | } |
| 1100 | /* check the channels */ |
| 1101 | if (fp->channels < ct->min || fp->channels > ct->max) { |
| 1102 | hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max); |
| 1103 | return 0; |
| 1104 | } |
| 1105 | /* check the rate is within the range */ |
| 1106 | if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) { |
| 1107 | hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max); |
| 1108 | return 0; |
| 1109 | } |
| 1110 | if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) { |
| 1111 | hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min); |
| 1112 | return 0; |
| 1113 | } |
| 1114 | /* check whether the period time is >= the data packet interval */ |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 1115 | if (subs->speed != USB_SPEED_FULL) { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1116 | ptime = 125 * (1 << fp->datainterval); |
| 1117 | if (ptime > pt->max || (ptime == pt->max && pt->openmax)) { |
| 1118 | hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max); |
| 1119 | return 0; |
| 1120 | } |
| 1121 | } |
| 1122 | return 1; |
| 1123 | } |
| 1124 | |
Takashi Iwai | 7726dce | 2020-11-23 09:53:17 +0100 | [diff] [blame] | 1125 | static int apply_hw_params_minmax(struct snd_interval *it, unsigned int rmin, |
| 1126 | unsigned int rmax) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1127 | { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1128 | int changed; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1129 | |
Takashi Iwai | bc4e94a | 2020-11-23 09:53:07 +0100 | [diff] [blame] | 1130 | if (rmin > rmax) { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1131 | hwc_debug(" --> get empty\n"); |
| 1132 | it->empty = 1; |
| 1133 | return -EINVAL; |
| 1134 | } |
| 1135 | |
| 1136 | changed = 0; |
| 1137 | if (it->min < rmin) { |
| 1138 | it->min = rmin; |
| 1139 | it->openmin = 0; |
| 1140 | changed = 1; |
| 1141 | } |
| 1142 | if (it->max > rmax) { |
| 1143 | it->max = rmax; |
| 1144 | it->openmax = 0; |
| 1145 | changed = 1; |
| 1146 | } |
| 1147 | if (snd_interval_checkempty(it)) { |
| 1148 | it->empty = 1; |
| 1149 | return -EINVAL; |
| 1150 | } |
| 1151 | hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed); |
| 1152 | return changed; |
| 1153 | } |
| 1154 | |
Takashi Iwai | 7726dce | 2020-11-23 09:53:17 +0100 | [diff] [blame] | 1155 | static int hw_rule_rate(struct snd_pcm_hw_params *params, |
| 1156 | struct snd_pcm_hw_rule *rule) |
| 1157 | { |
| 1158 | struct snd_usb_substream *subs = rule->private; |
| 1159 | struct audioformat *fp; |
| 1160 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); |
| 1161 | unsigned int rmin, rmax, r; |
| 1162 | int i; |
| 1163 | |
| 1164 | hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max); |
| 1165 | rmin = UINT_MAX; |
| 1166 | rmax = 0; |
| 1167 | list_for_each_entry(fp, &subs->fmt_list, list) { |
| 1168 | if (!hw_check_valid_format(subs, params, fp)) |
| 1169 | continue; |
| 1170 | if (fp->rate_table && fp->nr_rates) { |
| 1171 | for (i = 0; i < fp->nr_rates; i++) { |
| 1172 | r = fp->rate_table[i]; |
| 1173 | if (!snd_interval_test(it, r)) |
| 1174 | continue; |
| 1175 | rmin = min(rmin, r); |
| 1176 | rmax = max(rmax, r); |
| 1177 | } |
| 1178 | } else { |
| 1179 | rmin = min(rmin, fp->rate_min); |
| 1180 | rmax = max(rmax, fp->rate_max); |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | return apply_hw_params_minmax(it, rmin, rmax); |
| 1185 | } |
| 1186 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1187 | |
| 1188 | static int hw_rule_channels(struct snd_pcm_hw_params *params, |
| 1189 | struct snd_pcm_hw_rule *rule) |
| 1190 | { |
| 1191 | struct snd_usb_substream *subs = rule->private; |
Eldad Zack | 88766f0 | 2013-04-03 23:18:49 +0200 | [diff] [blame] | 1192 | struct audioformat *fp; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1193 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); |
| 1194 | unsigned int rmin, rmax; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1195 | |
| 1196 | hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max); |
Takashi Iwai | 7726dce | 2020-11-23 09:53:17 +0100 | [diff] [blame] | 1197 | rmin = UINT_MAX; |
| 1198 | rmax = 0; |
Eldad Zack | 88766f0 | 2013-04-03 23:18:49 +0200 | [diff] [blame] | 1199 | list_for_each_entry(fp, &subs->fmt_list, list) { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1200 | if (!hw_check_valid_format(subs, params, fp)) |
| 1201 | continue; |
Takashi Iwai | 7726dce | 2020-11-23 09:53:17 +0100 | [diff] [blame] | 1202 | rmin = min(rmin, fp->channels); |
| 1203 | rmax = max(rmax, fp->channels); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1204 | } |
| 1205 | |
Takashi Iwai | 7726dce | 2020-11-23 09:53:17 +0100 | [diff] [blame] | 1206 | return apply_hw_params_minmax(it, rmin, rmax); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | static int hw_rule_format(struct snd_pcm_hw_params *params, |
| 1210 | struct snd_pcm_hw_rule *rule) |
| 1211 | { |
| 1212 | struct snd_usb_substream *subs = rule->private; |
Eldad Zack | 88766f0 | 2013-04-03 23:18:49 +0200 | [diff] [blame] | 1213 | struct audioformat *fp; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1214 | struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); |
| 1215 | u64 fbits; |
| 1216 | u32 oldbits[2]; |
| 1217 | int changed; |
| 1218 | |
| 1219 | hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]); |
| 1220 | fbits = 0; |
Eldad Zack | 88766f0 | 2013-04-03 23:18:49 +0200 | [diff] [blame] | 1221 | list_for_each_entry(fp, &subs->fmt_list, list) { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1222 | if (!hw_check_valid_format(subs, params, fp)) |
| 1223 | continue; |
Clemens Ladisch | 015eb0b | 2010-03-04 19:46:15 +0100 | [diff] [blame] | 1224 | fbits |= fp->formats; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | oldbits[0] = fmt->bits[0]; |
| 1228 | oldbits[1] = fmt->bits[1]; |
| 1229 | fmt->bits[0] &= (u32)fbits; |
| 1230 | fmt->bits[1] &= (u32)(fbits >> 32); |
| 1231 | if (!fmt->bits[0] && !fmt->bits[1]) { |
| 1232 | hwc_debug(" --> get empty\n"); |
| 1233 | return -EINVAL; |
| 1234 | } |
| 1235 | changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]); |
| 1236 | hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed); |
| 1237 | return changed; |
| 1238 | } |
| 1239 | |
| 1240 | static int hw_rule_period_time(struct snd_pcm_hw_params *params, |
| 1241 | struct snd_pcm_hw_rule *rule) |
| 1242 | { |
| 1243 | struct snd_usb_substream *subs = rule->private; |
| 1244 | struct audioformat *fp; |
| 1245 | struct snd_interval *it; |
| 1246 | unsigned char min_datainterval; |
| 1247 | unsigned int pmin; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1248 | |
| 1249 | it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME); |
| 1250 | hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max); |
| 1251 | min_datainterval = 0xff; |
| 1252 | list_for_each_entry(fp, &subs->fmt_list, list) { |
| 1253 | if (!hw_check_valid_format(subs, params, fp)) |
| 1254 | continue; |
| 1255 | min_datainterval = min(min_datainterval, fp->datainterval); |
| 1256 | } |
| 1257 | if (min_datainterval == 0xff) { |
Uwe Kleine-König | a7ce2e0 | 2010-07-12 17:15:44 +0200 | [diff] [blame] | 1258 | hwc_debug(" --> get empty\n"); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1259 | it->empty = 1; |
| 1260 | return -EINVAL; |
| 1261 | } |
| 1262 | pmin = 125 * (1 << min_datainterval); |
Takashi Iwai | 7726dce | 2020-11-23 09:53:17 +0100 | [diff] [blame] | 1263 | |
| 1264 | return apply_hw_params_minmax(it, pmin, UINT_MAX); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1265 | } |
| 1266 | |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 1267 | /* apply PCM hw constraints from the concurrent sync EP */ |
| 1268 | static int apply_hw_constraint_from_sync(struct snd_pcm_runtime *runtime, |
| 1269 | struct snd_usb_substream *subs) |
| 1270 | { |
| 1271 | struct snd_usb_audio *chip = subs->stream->chip; |
| 1272 | struct snd_usb_endpoint *ep; |
| 1273 | struct audioformat *fp; |
| 1274 | int err; |
| 1275 | |
| 1276 | subs->fixed_hw = 0; |
| 1277 | list_for_each_entry(fp, &subs->fmt_list, list) { |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 1278 | ep = snd_usb_get_endpoint(chip, fp->endpoint); |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 1279 | if (ep && ep->cur_rate) |
| 1280 | goto found; |
| 1281 | if (!fp->implicit_fb) |
| 1282 | continue; |
| 1283 | /* for the implicit fb, check the sync ep as well */ |
Takashi Iwai | 54cb319 | 2020-11-23 09:53:20 +0100 | [diff] [blame] | 1284 | ep = snd_usb_get_endpoint(chip, fp->sync_ep); |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 1285 | if (ep && ep->cur_rate) |
| 1286 | goto found; |
| 1287 | } |
| 1288 | return 0; |
| 1289 | |
| 1290 | found: |
| 1291 | if (!find_format(&subs->fmt_list, ep->cur_format, ep->cur_rate, |
| 1292 | ep->cur_channels, NULL)) { |
| 1293 | usb_audio_dbg(chip, "EP 0x%x being used, but not applicable\n", |
| 1294 | ep->ep_num); |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | usb_audio_dbg(chip, "EP 0x%x being used, using fixed params:\n", |
| 1299 | ep->ep_num); |
| 1300 | usb_audio_dbg(chip, "rate=%d, format=%s, channels=%d, period_size=%d, periods=%d\n", |
| 1301 | ep->cur_rate, snd_pcm_format_name(ep->cur_format), |
| 1302 | ep->cur_channels, ep->cur_period_frames, |
| 1303 | ep->cur_buffer_periods); |
| 1304 | |
| 1305 | runtime->hw.formats = pcm_format_to_bits(ep->cur_format); |
| 1306 | runtime->hw.rate_min = runtime->hw.rate_max = ep->cur_rate; |
| 1307 | runtime->hw.channels_min = runtime->hw.channels_max = |
| 1308 | ep->cur_channels; |
| 1309 | runtime->hw.rates = SNDRV_PCM_RATE_KNOT; |
| 1310 | runtime->hw.periods_min = runtime->hw.periods_max = |
| 1311 | ep->cur_buffer_periods; |
| 1312 | subs->fixed_hw = 1; |
| 1313 | |
| 1314 | err = snd_pcm_hw_constraint_minmax(runtime, |
| 1315 | SNDRV_PCM_HW_PARAM_PERIOD_SIZE, |
| 1316 | ep->cur_period_frames, |
| 1317 | ep->cur_period_frames); |
| 1318 | if (err < 0) |
| 1319 | return err; |
| 1320 | |
| 1321 | return 1; /* notify the finding */ |
| 1322 | } |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1323 | |
| 1324 | /* |
| 1325 | * set up the runtime hardware information. |
| 1326 | */ |
| 1327 | |
| 1328 | static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs) |
| 1329 | { |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 1330 | struct snd_usb_audio *chip = subs->stream->chip; |
Eldad Zack | 88766f0 | 2013-04-03 23:18:49 +0200 | [diff] [blame] | 1331 | struct audioformat *fp; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1332 | unsigned int pt, ptmin; |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 1333 | int param_period_time_if_needed = -1; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1334 | int err; |
| 1335 | |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 1336 | mutex_lock(&chip->mutex); |
| 1337 | err = apply_hw_constraint_from_sync(runtime, subs); |
| 1338 | mutex_unlock(&chip->mutex); |
| 1339 | if (err < 0) |
| 1340 | return err; |
| 1341 | if (err > 0) /* found the matching? */ |
| 1342 | goto add_extra_rules; |
| 1343 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1344 | runtime->hw.formats = subs->formats; |
| 1345 | |
| 1346 | runtime->hw.rate_min = 0x7fffffff; |
| 1347 | runtime->hw.rate_max = 0; |
| 1348 | runtime->hw.channels_min = 256; |
| 1349 | runtime->hw.channels_max = 0; |
| 1350 | runtime->hw.rates = 0; |
| 1351 | ptmin = UINT_MAX; |
| 1352 | /* check min/max rates and channels */ |
Eldad Zack | 88766f0 | 2013-04-03 23:18:49 +0200 | [diff] [blame] | 1353 | list_for_each_entry(fp, &subs->fmt_list, list) { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1354 | runtime->hw.rates |= fp->rates; |
| 1355 | if (runtime->hw.rate_min > fp->rate_min) |
| 1356 | runtime->hw.rate_min = fp->rate_min; |
| 1357 | if (runtime->hw.rate_max < fp->rate_max) |
| 1358 | runtime->hw.rate_max = fp->rate_max; |
| 1359 | if (runtime->hw.channels_min > fp->channels) |
| 1360 | runtime->hw.channels_min = fp->channels; |
| 1361 | if (runtime->hw.channels_max < fp->channels) |
| 1362 | runtime->hw.channels_max = fp->channels; |
| 1363 | if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) { |
| 1364 | /* FIXME: there might be more than one audio formats... */ |
| 1365 | runtime->hw.period_bytes_min = runtime->hw.period_bytes_max = |
| 1366 | fp->frame_size; |
| 1367 | } |
| 1368 | pt = 125 * (1 << fp->datainterval); |
| 1369 | ptmin = min(ptmin, pt); |
| 1370 | } |
| 1371 | |
| 1372 | param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME; |
Takashi Iwai | 978520b | 2012-10-12 15:12:55 +0200 | [diff] [blame] | 1373 | if (subs->speed == USB_SPEED_FULL) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1374 | /* full speed devices have fixed data packet interval */ |
| 1375 | ptmin = 1000; |
| 1376 | if (ptmin == 1000) |
| 1377 | /* if period time doesn't go below 1 ms, no rules needed */ |
| 1378 | param_period_time_if_needed = -1; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1379 | |
Takashi Iwai | e92be81 | 2018-05-27 15:09:15 +0200 | [diff] [blame] | 1380 | err = snd_pcm_hw_constraint_minmax(runtime, |
| 1381 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, |
| 1382 | ptmin, UINT_MAX); |
| 1383 | if (err < 0) |
| 1384 | return err; |
| 1385 | |
| 1386 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, |
| 1387 | hw_rule_rate, subs, |
| 1388 | SNDRV_PCM_HW_PARAM_FORMAT, |
| 1389 | SNDRV_PCM_HW_PARAM_CHANNELS, |
| 1390 | param_period_time_if_needed, |
| 1391 | -1); |
| 1392 | if (err < 0) |
| 1393 | return err; |
Takashi Iwai | 5a6c3e1 | 2020-11-23 09:53:16 +0100 | [diff] [blame] | 1394 | |
| 1395 | add_extra_rules: |
Takashi Iwai | e92be81 | 2018-05-27 15:09:15 +0200 | [diff] [blame] | 1396 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, |
| 1397 | hw_rule_channels, subs, |
| 1398 | SNDRV_PCM_HW_PARAM_FORMAT, |
| 1399 | SNDRV_PCM_HW_PARAM_RATE, |
| 1400 | param_period_time_if_needed, |
| 1401 | -1); |
| 1402 | if (err < 0) |
| 1403 | return err; |
| 1404 | err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, |
| 1405 | hw_rule_format, subs, |
| 1406 | SNDRV_PCM_HW_PARAM_RATE, |
| 1407 | SNDRV_PCM_HW_PARAM_CHANNELS, |
| 1408 | param_period_time_if_needed, |
| 1409 | -1); |
| 1410 | if (err < 0) |
| 1411 | return err; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1412 | if (param_period_time_if_needed >= 0) { |
| 1413 | err = snd_pcm_hw_rule_add(runtime, 0, |
| 1414 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, |
| 1415 | hw_rule_period_time, subs, |
| 1416 | SNDRV_PCM_HW_PARAM_FORMAT, |
| 1417 | SNDRV_PCM_HW_PARAM_CHANNELS, |
| 1418 | SNDRV_PCM_HW_PARAM_RATE, |
| 1419 | -1); |
| 1420 | if (err < 0) |
Takashi Iwai | e92be81 | 2018-05-27 15:09:15 +0200 | [diff] [blame] | 1421 | return err; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1422 | } |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 1423 | |
Takashi Iwai | 1865211 | 2020-11-23 09:53:15 +0100 | [diff] [blame] | 1424 | return 0; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1425 | } |
| 1426 | |
Takashi Iwai | 6fddc79 | 2018-05-27 13:59:03 +0200 | [diff] [blame] | 1427 | static int snd_usb_pcm_open(struct snd_pcm_substream *substream) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1428 | { |
Takashi Iwai | 6fddc79 | 2018-05-27 13:59:03 +0200 | [diff] [blame] | 1429 | int direction = substream->stream; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1430 | struct snd_usb_stream *as = snd_pcm_substream_chip(substream); |
| 1431 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 1432 | struct snd_usb_substream *subs = &as->substream[direction]; |
Shuah Khan | 66354f1 | 2019-04-01 20:40:22 -0400 | [diff] [blame] | 1433 | int ret; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1434 | |
| 1435 | subs->interface = -1; |
Clemens Ladisch | e11b4e0 | 2010-03-04 19:46:14 +0100 | [diff] [blame] | 1436 | subs->altset_idx = 0; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1437 | runtime->hw = snd_usb_hardware; |
| 1438 | runtime->private_data = subs; |
| 1439 | subs->pcm_substream = substream; |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 1440 | /* runtime PM is also done there */ |
Daniel Mack | d24f506 | 2013-04-17 00:01:38 +0800 | [diff] [blame] | 1441 | |
| 1442 | /* initialize DSD/DOP context */ |
| 1443 | subs->dsd_dop.byte_idx = 0; |
| 1444 | subs->dsd_dop.channel = 0; |
| 1445 | subs->dsd_dop.marker = 1; |
| 1446 | |
Shuah Khan | 66354f1 | 2019-04-01 20:40:22 -0400 | [diff] [blame] | 1447 | ret = setup_hw_info(runtime, subs); |
Takashi Iwai | 1865211 | 2020-11-23 09:53:15 +0100 | [diff] [blame] | 1448 | if (ret < 0) |
| 1449 | return ret; |
| 1450 | ret = snd_usb_autoresume(subs->stream->chip); |
| 1451 | if (ret < 0) |
| 1452 | return ret; |
| 1453 | ret = snd_media_stream_init(subs, as->pcm, direction); |
| 1454 | if (ret < 0) |
| 1455 | snd_usb_autosuspend(subs->stream->chip); |
Shuah Khan | 66354f1 | 2019-04-01 20:40:22 -0400 | [diff] [blame] | 1456 | return ret; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1457 | } |
| 1458 | |
Takashi Iwai | 6fddc79 | 2018-05-27 13:59:03 +0200 | [diff] [blame] | 1459 | static int snd_usb_pcm_close(struct snd_pcm_substream *substream) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1460 | { |
Takashi Iwai | 6fddc79 | 2018-05-27 13:59:03 +0200 | [diff] [blame] | 1461 | int direction = substream->stream; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1462 | struct snd_usb_stream *as = snd_pcm_substream_chip(substream); |
| 1463 | struct snd_usb_substream *subs = &as->substream[direction]; |
Jorge Sanjuan | a0a4959 | 2018-07-31 13:28:45 +0100 | [diff] [blame] | 1464 | int ret; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1465 | |
Shuah Khan | 66354f1 | 2019-04-01 20:40:22 -0400 | [diff] [blame] | 1466 | snd_media_stop_pipeline(subs); |
Daniel Mack | 68e67f4 | 2012-07-12 13:08:40 +0200 | [diff] [blame] | 1467 | |
Takashi Iwai | 9821505 | 2020-11-23 09:53:21 +0100 | [diff] [blame] | 1468 | if (subs->interface >= 0 && |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 1469 | !snd_usb_lock_shutdown(subs->stream->chip)) { |
Daniel Mack | 68e67f4 | 2012-07-12 13:08:40 +0200 | [diff] [blame] | 1470 | usb_set_interface(subs->dev, subs->interface, 0); |
| 1471 | subs->interface = -1; |
Jorge Sanjuan | a0a4959 | 2018-07-31 13:28:45 +0100 | [diff] [blame] | 1472 | ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D1); |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 1473 | snd_usb_unlock_shutdown(subs->stream->chip); |
Jorge Sanjuan | a0a4959 | 2018-07-31 13:28:45 +0100 | [diff] [blame] | 1474 | if (ret < 0) |
| 1475 | return ret; |
Daniel Mack | 68e67f4 | 2012-07-12 13:08:40 +0200 | [diff] [blame] | 1476 | } |
| 1477 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1478 | subs->pcm_substream = NULL; |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 1479 | snd_usb_autosuspend(subs->stream->chip); |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1480 | |
Daniel Mack | 68e67f4 | 2012-07-12 13:08:40 +0200 | [diff] [blame] | 1481 | return 0; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | /* Since a URB can handle only a single linear buffer, we must use double |
| 1485 | * buffering when the data to be transferred overflows the buffer boundary. |
| 1486 | * To avoid inconsistencies when updating hwptr_done, we use double buffering |
| 1487 | * for all URBs. |
| 1488 | */ |
| 1489 | static void retire_capture_urb(struct snd_usb_substream *subs, |
| 1490 | struct urb *urb) |
| 1491 | { |
| 1492 | struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; |
| 1493 | unsigned int stride, frames, bytes, oldptr; |
| 1494 | int i, period_elapsed = 0; |
| 1495 | unsigned long flags; |
| 1496 | unsigned char *cp; |
Pierre-Louis Bossart | e4cc615 | 2012-12-19 11:39:05 -0600 | [diff] [blame] | 1497 | int current_frame_number; |
| 1498 | |
| 1499 | /* read frame number here, update pointer in critical section */ |
| 1500 | current_frame_number = usb_get_current_frame_number(subs->dev); |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1501 | |
| 1502 | stride = runtime->frame_bits >> 3; |
| 1503 | |
| 1504 | for (i = 0; i < urb->number_of_packets; i++) { |
Calvin Owens | 1539d4f | 2013-04-12 22:33:59 -0500 | [diff] [blame] | 1505 | cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1506 | if (urb->iso_frame_desc[i].status && printk_ratelimit()) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1507 | dev_dbg(&subs->dev->dev, "frame %d active: %d\n", |
| 1508 | i, urb->iso_frame_desc[i].status); |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1509 | // continue; |
| 1510 | } |
| 1511 | bytes = urb->iso_frame_desc[i].actual_length; |
Hector Martin | 1b7ecc2 | 2020-08-10 17:24:00 +0900 | [diff] [blame] | 1512 | if (subs->stream_offset_adj > 0) { |
| 1513 | unsigned int adj = min(subs->stream_offset_adj, bytes); |
| 1514 | cp += adj; |
| 1515 | bytes -= adj; |
| 1516 | subs->stream_offset_adj -= adj; |
| 1517 | } |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1518 | frames = bytes / stride; |
| 1519 | if (!subs->txfr_quirk) |
| 1520 | bytes = frames * stride; |
| 1521 | if (bytes % (runtime->sample_bits >> 3) != 0) { |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1522 | int oldbytes = bytes; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1523 | bytes = frames * stride; |
Takashi Iwai | 377a879 | 2018-05-16 20:07:18 +0200 | [diff] [blame] | 1524 | dev_warn_ratelimited(&subs->dev->dev, |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1525 | "Corrected urb data len. %d->%d\n", |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1526 | oldbytes, bytes); |
| 1527 | } |
| 1528 | /* update the current pointer */ |
| 1529 | spin_lock_irqsave(&subs->lock, flags); |
| 1530 | oldptr = subs->hwptr_done; |
| 1531 | subs->hwptr_done += bytes; |
| 1532 | if (subs->hwptr_done >= runtime->buffer_size * stride) |
| 1533 | subs->hwptr_done -= runtime->buffer_size * stride; |
| 1534 | frames = (bytes + (oldptr % stride)) / stride; |
| 1535 | subs->transfer_done += frames; |
| 1536 | if (subs->transfer_done >= runtime->period_size) { |
| 1537 | subs->transfer_done -= runtime->period_size; |
| 1538 | period_elapsed = 1; |
| 1539 | } |
Pierre-Louis Bossart | e4cc615 | 2012-12-19 11:39:05 -0600 | [diff] [blame] | 1540 | /* capture delay is by construction limited to one URB, |
| 1541 | * reset delays here |
| 1542 | */ |
| 1543 | runtime->delay = subs->last_delay = 0; |
| 1544 | |
| 1545 | /* realign last_frame_number */ |
| 1546 | subs->last_frame_number = current_frame_number; |
| 1547 | subs->last_frame_number &= 0xFF; /* keep 8 LSBs */ |
| 1548 | |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1549 | spin_unlock_irqrestore(&subs->lock, flags); |
| 1550 | /* copy a data chunk */ |
| 1551 | if (oldptr + bytes > runtime->buffer_size * stride) { |
| 1552 | unsigned int bytes1 = |
| 1553 | runtime->buffer_size * stride - oldptr; |
| 1554 | memcpy(runtime->dma_area + oldptr, cp, bytes1); |
| 1555 | memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1); |
| 1556 | } else { |
| 1557 | memcpy(runtime->dma_area + oldptr, cp, bytes); |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | if (period_elapsed) |
| 1562 | snd_pcm_period_elapsed(subs->pcm_substream); |
| 1563 | } |
| 1564 | |
Daniel Mack | d24f506 | 2013-04-17 00:01:38 +0800 | [diff] [blame] | 1565 | static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs, |
| 1566 | struct urb *urb, unsigned int bytes) |
| 1567 | { |
| 1568 | struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; |
| 1569 | unsigned int stride = runtime->frame_bits >> 3; |
| 1570 | unsigned int dst_idx = 0; |
| 1571 | unsigned int src_idx = subs->hwptr_done; |
| 1572 | unsigned int wrap = runtime->buffer_size * stride; |
| 1573 | u8 *dst = urb->transfer_buffer; |
| 1574 | u8 *src = runtime->dma_area; |
| 1575 | u8 marker[] = { 0x05, 0xfa }; |
| 1576 | |
| 1577 | /* |
| 1578 | * The DSP DOP format defines a way to transport DSD samples over |
| 1579 | * normal PCM data endpoints. It requires stuffing of marker bytes |
| 1580 | * (0x05 and 0xfa, alternating per sample frame), and then expects |
| 1581 | * 2 additional bytes of actual payload. The whole frame is stored |
| 1582 | * LSB. |
| 1583 | * |
| 1584 | * Hence, for a stereo transport, the buffer layout looks like this, |
| 1585 | * where L refers to left channel samples and R to right. |
| 1586 | * |
| 1587 | * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa |
| 1588 | * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa |
| 1589 | * ..... |
| 1590 | * |
| 1591 | */ |
| 1592 | |
| 1593 | while (bytes--) { |
| 1594 | if (++subs->dsd_dop.byte_idx == 3) { |
| 1595 | /* frame boundary? */ |
| 1596 | dst[dst_idx++] = marker[subs->dsd_dop.marker]; |
| 1597 | src_idx += 2; |
| 1598 | subs->dsd_dop.byte_idx = 0; |
| 1599 | |
| 1600 | if (++subs->dsd_dop.channel % runtime->channels == 0) { |
| 1601 | /* alternate the marker */ |
| 1602 | subs->dsd_dop.marker++; |
| 1603 | subs->dsd_dop.marker %= ARRAY_SIZE(marker); |
| 1604 | subs->dsd_dop.channel = 0; |
| 1605 | } |
| 1606 | } else { |
| 1607 | /* stuff the DSD payload */ |
| 1608 | int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap; |
Daniel Mack | 44dcbbb | 2013-04-17 00:01:39 +0800 | [diff] [blame] | 1609 | |
| 1610 | if (subs->cur_audiofmt->dsd_bitrev) |
| 1611 | dst[dst_idx++] = bitrev8(src[idx]); |
| 1612 | else |
| 1613 | dst[dst_idx++] = src[idx]; |
| 1614 | |
Daniel Mack | d24f506 | 2013-04-17 00:01:38 +0800 | [diff] [blame] | 1615 | subs->hwptr_done++; |
| 1616 | } |
| 1617 | } |
Ricard Wanderlof | 4c4e439 | 2015-10-19 08:52:50 +0200 | [diff] [blame] | 1618 | if (subs->hwptr_done >= runtime->buffer_size * stride) |
| 1619 | subs->hwptr_done -= runtime->buffer_size * stride; |
Daniel Mack | d24f506 | 2013-04-17 00:01:38 +0800 | [diff] [blame] | 1620 | } |
| 1621 | |
Ricard Wanderlof | b97a936 | 2015-10-19 08:52:52 +0200 | [diff] [blame] | 1622 | static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb, |
| 1623 | int offset, int stride, unsigned int bytes) |
Ricard Wanderlof | 07a40c2f | 2015-10-19 08:52:49 +0200 | [diff] [blame] | 1624 | { |
| 1625 | struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; |
| 1626 | |
| 1627 | if (subs->hwptr_done + bytes > runtime->buffer_size * stride) { |
| 1628 | /* err, the transferred area goes over buffer boundary. */ |
| 1629 | unsigned int bytes1 = |
| 1630 | runtime->buffer_size * stride - subs->hwptr_done; |
Ricard Wanderlof | b97a936 | 2015-10-19 08:52:52 +0200 | [diff] [blame] | 1631 | memcpy(urb->transfer_buffer + offset, |
Ricard Wanderlof | 07a40c2f | 2015-10-19 08:52:49 +0200 | [diff] [blame] | 1632 | runtime->dma_area + subs->hwptr_done, bytes1); |
Ricard Wanderlof | b97a936 | 2015-10-19 08:52:52 +0200 | [diff] [blame] | 1633 | memcpy(urb->transfer_buffer + offset + bytes1, |
Ricard Wanderlof | 07a40c2f | 2015-10-19 08:52:49 +0200 | [diff] [blame] | 1634 | runtime->dma_area, bytes - bytes1); |
| 1635 | } else { |
Ricard Wanderlof | b97a936 | 2015-10-19 08:52:52 +0200 | [diff] [blame] | 1636 | memcpy(urb->transfer_buffer + offset, |
Ricard Wanderlof | 07a40c2f | 2015-10-19 08:52:49 +0200 | [diff] [blame] | 1637 | runtime->dma_area + subs->hwptr_done, bytes); |
| 1638 | } |
| 1639 | subs->hwptr_done += bytes; |
Ricard Wanderlof | 4c4e439 | 2015-10-19 08:52:50 +0200 | [diff] [blame] | 1640 | if (subs->hwptr_done >= runtime->buffer_size * stride) |
| 1641 | subs->hwptr_done -= runtime->buffer_size * stride; |
Ricard Wanderlof | 07a40c2f | 2015-10-19 08:52:49 +0200 | [diff] [blame] | 1642 | } |
| 1643 | |
Ricard Wanderlof | e057044 | 2015-10-19 08:52:53 +0200 | [diff] [blame] | 1644 | static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs, |
| 1645 | struct urb *urb, int stride, |
| 1646 | unsigned int bytes) |
| 1647 | { |
| 1648 | __le32 packet_length; |
| 1649 | int i; |
| 1650 | |
| 1651 | /* Put __le32 length descriptor at start of each packet. */ |
| 1652 | for (i = 0; i < urb->number_of_packets; i++) { |
| 1653 | unsigned int length = urb->iso_frame_desc[i].length; |
| 1654 | unsigned int offset = urb->iso_frame_desc[i].offset; |
| 1655 | |
| 1656 | packet_length = cpu_to_le32(length); |
| 1657 | offset += i * sizeof(packet_length); |
| 1658 | urb->iso_frame_desc[i].offset = offset; |
| 1659 | urb->iso_frame_desc[i].length += sizeof(packet_length); |
| 1660 | memcpy(urb->transfer_buffer + offset, |
| 1661 | &packet_length, sizeof(packet_length)); |
| 1662 | copy_to_urb(subs, urb, offset + sizeof(packet_length), |
| 1663 | stride, length); |
| 1664 | } |
| 1665 | /* Adjust transfer size accordingly. */ |
| 1666 | bytes += urb->number_of_packets * sizeof(packet_length); |
| 1667 | return bytes; |
| 1668 | } |
| 1669 | |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1670 | static void prepare_playback_urb(struct snd_usb_substream *subs, |
| 1671 | struct urb *urb) |
| 1672 | { |
| 1673 | struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; |
Daniel Mack | 245baf9 | 2012-08-30 18:52:30 +0200 | [diff] [blame] | 1674 | struct snd_usb_endpoint *ep = subs->data_endpoint; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1675 | struct snd_urb_ctx *ctx = urb->context; |
| 1676 | unsigned int counts, frames, bytes; |
| 1677 | int i, stride, period_elapsed = 0; |
| 1678 | unsigned long flags; |
| 1679 | |
| 1680 | stride = runtime->frame_bits >> 3; |
| 1681 | |
| 1682 | frames = 0; |
| 1683 | urb->number_of_packets = 0; |
| 1684 | spin_lock_irqsave(&subs->lock, flags); |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 1685 | subs->frame_limit += ep->max_urb_frames; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1686 | for (i = 0; i < ctx->packets; i++) { |
Daniel Mack | 245baf9 | 2012-08-30 18:52:30 +0200 | [diff] [blame] | 1687 | if (ctx->packet_size[i]) |
| 1688 | counts = ctx->packet_size[i]; |
Alexander Tsoy | f0bd62b | 2020-04-24 05:24:48 +0300 | [diff] [blame] | 1689 | else if (ep->sync_master) |
| 1690 | counts = snd_usb_endpoint_slave_next_packet_size(ep); |
Daniel Mack | 245baf9 | 2012-08-30 18:52:30 +0200 | [diff] [blame] | 1691 | else |
| 1692 | counts = snd_usb_endpoint_next_packet_size(ep); |
| 1693 | |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1694 | /* set up descriptor */ |
Daniel Mack | 8a2a74d | 2013-04-17 00:01:37 +0800 | [diff] [blame] | 1695 | urb->iso_frame_desc[i].offset = frames * ep->stride; |
| 1696 | urb->iso_frame_desc[i].length = counts * ep->stride; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1697 | frames += counts; |
| 1698 | urb->number_of_packets++; |
| 1699 | subs->transfer_done += counts; |
| 1700 | if (subs->transfer_done >= runtime->period_size) { |
| 1701 | subs->transfer_done -= runtime->period_size; |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 1702 | subs->frame_limit = 0; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1703 | period_elapsed = 1; |
| 1704 | if (subs->fmt_type == UAC_FORMAT_TYPE_II) { |
| 1705 | if (subs->transfer_done > 0) { |
| 1706 | /* FIXME: fill-max mode is not |
| 1707 | * supported yet */ |
| 1708 | frames -= subs->transfer_done; |
| 1709 | counts -= subs->transfer_done; |
| 1710 | urb->iso_frame_desc[i].length = |
Daniel Mack | 8a2a74d | 2013-04-17 00:01:37 +0800 | [diff] [blame] | 1711 | counts * ep->stride; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1712 | subs->transfer_done = 0; |
| 1713 | } |
| 1714 | i++; |
| 1715 | if (i < ctx->packets) { |
| 1716 | /* add a transfer delimiter */ |
| 1717 | urb->iso_frame_desc[i].offset = |
Daniel Mack | 8a2a74d | 2013-04-17 00:01:37 +0800 | [diff] [blame] | 1718 | frames * ep->stride; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1719 | urb->iso_frame_desc[i].length = 0; |
| 1720 | urb->number_of_packets++; |
| 1721 | } |
| 1722 | break; |
| 1723 | } |
| 1724 | } |
Alan Stern | 976b6c0 | 2013-09-24 15:51:58 -0400 | [diff] [blame] | 1725 | /* finish at the period boundary or after enough frames */ |
| 1726 | if ((period_elapsed || |
| 1727 | subs->transfer_done >= subs->frame_limit) && |
| 1728 | !snd_usb_endpoint_implicit_feedback_sink(ep)) |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1729 | break; |
| 1730 | } |
Daniel Mack | 8a2a74d | 2013-04-17 00:01:37 +0800 | [diff] [blame] | 1731 | bytes = frames * ep->stride; |
Daniel Mack | d24f506 | 2013-04-17 00:01:38 +0800 | [diff] [blame] | 1732 | |
| 1733 | if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE && |
| 1734 | subs->cur_audiofmt->dsd_dop)) { |
| 1735 | fill_playback_urb_dsd_dop(subs, urb, bytes); |
Daniel Mack | 44dcbbb | 2013-04-17 00:01:39 +0800 | [diff] [blame] | 1736 | } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 && |
| 1737 | subs->cur_audiofmt->dsd_bitrev)) { |
| 1738 | /* bit-reverse the bytes */ |
| 1739 | u8 *buf = urb->transfer_buffer; |
| 1740 | for (i = 0; i < bytes; i++) { |
| 1741 | int idx = (subs->hwptr_done + i) |
| 1742 | % (runtime->buffer_size * stride); |
| 1743 | buf[i] = bitrev8(runtime->dma_area[idx]); |
| 1744 | } |
| 1745 | |
| 1746 | subs->hwptr_done += bytes; |
Ricard Wanderlof | 4c4e439 | 2015-10-19 08:52:50 +0200 | [diff] [blame] | 1747 | if (subs->hwptr_done >= runtime->buffer_size * stride) |
| 1748 | subs->hwptr_done -= runtime->buffer_size * stride; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1749 | } else { |
Daniel Mack | d24f506 | 2013-04-17 00:01:38 +0800 | [diff] [blame] | 1750 | /* usual PCM */ |
Ricard Wanderlof | e057044 | 2015-10-19 08:52:53 +0200 | [diff] [blame] | 1751 | if (!subs->tx_length_quirk) |
| 1752 | copy_to_urb(subs, urb, 0, stride, bytes); |
| 1753 | else |
| 1754 | bytes = copy_to_urb_quirk(subs, urb, stride, bytes); |
| 1755 | /* bytes is now amount of outgoing data */ |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1756 | } |
Daniel Mack | d24f506 | 2013-04-17 00:01:38 +0800 | [diff] [blame] | 1757 | |
Daniel Mack | fbcfbf5 | 2012-08-30 18:52:29 +0200 | [diff] [blame] | 1758 | /* update delay with exact number of samples queued */ |
| 1759 | runtime->delay = subs->last_delay; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1760 | runtime->delay += frames; |
Daniel Mack | fbcfbf5 | 2012-08-30 18:52:29 +0200 | [diff] [blame] | 1761 | subs->last_delay = runtime->delay; |
| 1762 | |
| 1763 | /* realign last_frame_number */ |
| 1764 | subs->last_frame_number = usb_get_current_frame_number(subs->dev); |
| 1765 | subs->last_frame_number &= 0xFF; /* keep 8 LSBs */ |
| 1766 | |
Pierre-Louis Bossart | ea33d35 | 2015-02-06 15:55:53 -0600 | [diff] [blame] | 1767 | if (subs->trigger_tstamp_pending_update) { |
| 1768 | /* this is the first actual URB submitted, |
| 1769 | * update trigger timestamp to reflect actual start time |
| 1770 | */ |
| 1771 | snd_pcm_gettime(runtime, &runtime->trigger_tstamp); |
| 1772 | subs->trigger_tstamp_pending_update = false; |
| 1773 | } |
| 1774 | |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1775 | spin_unlock_irqrestore(&subs->lock, flags); |
| 1776 | urb->transfer_buffer_length = bytes; |
| 1777 | if (period_elapsed) |
| 1778 | snd_pcm_period_elapsed(subs->pcm_substream); |
| 1779 | } |
| 1780 | |
| 1781 | /* |
| 1782 | * process after playback data complete |
| 1783 | * - decrease the delay count again |
| 1784 | */ |
| 1785 | static void retire_playback_urb(struct snd_usb_substream *subs, |
| 1786 | struct urb *urb) |
| 1787 | { |
| 1788 | unsigned long flags; |
| 1789 | struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; |
Daniel Mack | 8a2a74d | 2013-04-17 00:01:37 +0800 | [diff] [blame] | 1790 | struct snd_usb_endpoint *ep = subs->data_endpoint; |
| 1791 | int processed = urb->transfer_buffer_length / ep->stride; |
Daniel Mack | fbcfbf5 | 2012-08-30 18:52:29 +0200 | [diff] [blame] | 1792 | int est_delay; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1793 | |
Alexander Tsoy | 5ff40e6 | 2020-06-29 06:26:07 +0300 | [diff] [blame] | 1794 | /* ignore the delay accounting when processed=0 is given, i.e. |
| 1795 | * silent payloads are processed before handling the actual data |
Takashi Iwai | 1213a20 | 2012-09-06 14:58:00 +0200 | [diff] [blame] | 1796 | */ |
| 1797 | if (!processed) |
| 1798 | return; |
| 1799 | |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1800 | spin_lock_irqsave(&subs->lock, flags); |
Takashi Iwai | 48779a0 | 2012-11-23 16:00:37 +0100 | [diff] [blame] | 1801 | if (!subs->last_delay) |
| 1802 | goto out; /* short path */ |
| 1803 | |
Daniel Mack | fbcfbf5 | 2012-08-30 18:52:29 +0200 | [diff] [blame] | 1804 | est_delay = snd_usb_pcm_delay(subs, runtime->rate); |
| 1805 | /* update delay with exact number of samples played */ |
| 1806 | if (processed > subs->last_delay) |
| 1807 | subs->last_delay = 0; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1808 | else |
Daniel Mack | fbcfbf5 | 2012-08-30 18:52:29 +0200 | [diff] [blame] | 1809 | subs->last_delay -= processed; |
| 1810 | runtime->delay = subs->last_delay; |
| 1811 | |
| 1812 | /* |
| 1813 | * Report when delay estimate is off by more than 2ms. |
| 1814 | * The error should be lower than 2ms since the estimate relies |
| 1815 | * on two reads of a counter updated every ms. |
| 1816 | */ |
Sander Eikelenboom | b7a7723 | 2014-05-02 15:09:27 +0200 | [diff] [blame] | 1817 | if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2) |
| 1818 | dev_dbg_ratelimited(&subs->dev->dev, |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 1819 | "delay: estimated %d, actual %d\n", |
Daniel Mack | fbcfbf5 | 2012-08-30 18:52:29 +0200 | [diff] [blame] | 1820 | est_delay, subs->last_delay); |
| 1821 | |
Takashi Iwai | 48779a0 | 2012-11-23 16:00:37 +0100 | [diff] [blame] | 1822 | if (!subs->running) { |
| 1823 | /* update last_frame_number for delay counting here since |
| 1824 | * prepare_playback_urb won't be called during pause |
| 1825 | */ |
| 1826 | subs->last_frame_number = |
| 1827 | usb_get_current_frame_number(subs->dev) & 0xff; |
| 1828 | } |
| 1829 | |
| 1830 | out: |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1831 | spin_unlock_irqrestore(&subs->lock, flags); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1832 | } |
| 1833 | |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1834 | static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, |
| 1835 | int cmd) |
| 1836 | { |
| 1837 | struct snd_usb_substream *subs = substream->runtime->private_data; |
| 1838 | |
| 1839 | switch (cmd) { |
| 1840 | case SNDRV_PCM_TRIGGER_START: |
Pierre-Louis Bossart | ea33d35 | 2015-02-06 15:55:53 -0600 | [diff] [blame] | 1841 | subs->trigger_tstamp_pending_update = true; |
Gustavo A. R. Silva | c0dbbda | 2020-07-08 15:32:36 -0500 | [diff] [blame] | 1842 | fallthrough; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1843 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 1844 | subs->data_endpoint->prepare_data_urb = prepare_playback_urb; |
| 1845 | subs->data_endpoint->retire_data_urb = retire_playback_urb; |
Daniel Mack | 97f8d3b | 2012-05-21 12:47:36 +0200 | [diff] [blame] | 1846 | subs->running = 1; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1847 | return 0; |
| 1848 | case SNDRV_PCM_TRIGGER_STOP: |
Takashi Iwai | dc5eafe | 2019-12-10 07:34:54 +0100 | [diff] [blame] | 1849 | stop_endpoints(subs); |
Daniel Mack | 97f8d3b | 2012-05-21 12:47:36 +0200 | [diff] [blame] | 1850 | subs->running = 0; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1851 | return 0; |
| 1852 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 1853 | subs->data_endpoint->prepare_data_urb = NULL; |
Takashi Iwai | 48779a0 | 2012-11-23 16:00:37 +0100 | [diff] [blame] | 1854 | /* keep retire_data_urb for delay calculation */ |
| 1855 | subs->data_endpoint->retire_data_urb = retire_playback_urb; |
Daniel Mack | 97f8d3b | 2012-05-21 12:47:36 +0200 | [diff] [blame] | 1856 | subs->running = 0; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1857 | return 0; |
Hui Wang | 92adc96 | 2019-12-18 21:26:50 +0800 | [diff] [blame] | 1858 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 1859 | if (subs->stream->chip->setup_fmt_after_resume_quirk) { |
Takashi Iwai | a032ff0 | 2019-12-18 20:05:39 +0100 | [diff] [blame] | 1860 | stop_endpoints(subs); |
Hui Wang | 92adc96 | 2019-12-18 21:26:50 +0800 | [diff] [blame] | 1861 | subs->need_setup_fmt = true; |
| 1862 | return 0; |
| 1863 | } |
| 1864 | break; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | return -EINVAL; |
| 1868 | } |
| 1869 | |
Daniel Mack | afe2596 | 2012-06-16 16:58:04 +0200 | [diff] [blame] | 1870 | static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, |
| 1871 | int cmd) |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1872 | { |
| 1873 | int err; |
| 1874 | struct snd_usb_substream *subs = substream->runtime->private_data; |
| 1875 | |
| 1876 | switch (cmd) { |
| 1877 | case SNDRV_PCM_TRIGGER_START: |
Ioan-Adrian Ratiu | 1d0f953 | 2017-01-05 00:37:46 +0200 | [diff] [blame] | 1878 | err = start_endpoints(subs); |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1879 | if (err < 0) |
| 1880 | return err; |
| 1881 | |
| 1882 | subs->data_endpoint->retire_data_urb = retire_capture_urb; |
Daniel Mack | 97f8d3b | 2012-05-21 12:47:36 +0200 | [diff] [blame] | 1883 | subs->running = 1; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1884 | return 0; |
| 1885 | case SNDRV_PCM_TRIGGER_STOP: |
Takashi Iwai | dc5eafe | 2019-12-10 07:34:54 +0100 | [diff] [blame] | 1886 | stop_endpoints(subs); |
Takashi Iwai | ff58bbc | 2020-06-16 14:09:21 +0200 | [diff] [blame] | 1887 | subs->data_endpoint->retire_data_urb = NULL; |
Daniel Mack | 97f8d3b | 2012-05-21 12:47:36 +0200 | [diff] [blame] | 1888 | subs->running = 0; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1889 | return 0; |
| 1890 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 1891 | subs->data_endpoint->retire_data_urb = NULL; |
Daniel Mack | 97f8d3b | 2012-05-21 12:47:36 +0200 | [diff] [blame] | 1892 | subs->running = 0; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1893 | return 0; |
| 1894 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 1895 | subs->data_endpoint->retire_data_urb = retire_capture_urb; |
Daniel Mack | 97f8d3b | 2012-05-21 12:47:36 +0200 | [diff] [blame] | 1896 | subs->running = 1; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1897 | return 0; |
Hui Wang | 92adc96 | 2019-12-18 21:26:50 +0800 | [diff] [blame] | 1898 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 1899 | if (subs->stream->chip->setup_fmt_after_resume_quirk) { |
Takashi Iwai | a032ff0 | 2019-12-18 20:05:39 +0100 | [diff] [blame] | 1900 | stop_endpoints(subs); |
Hui Wang | 92adc96 | 2019-12-18 21:26:50 +0800 | [diff] [blame] | 1901 | subs->need_setup_fmt = true; |
| 1902 | return 0; |
| 1903 | } |
| 1904 | break; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 1905 | } |
| 1906 | |
| 1907 | return -EINVAL; |
| 1908 | } |
| 1909 | |
Arvind Yadav | 31cb1fb | 2017-08-18 13:15:21 +0530 | [diff] [blame] | 1910 | static const struct snd_pcm_ops snd_usb_playback_ops = { |
Takashi Iwai | 6fddc79 | 2018-05-27 13:59:03 +0200 | [diff] [blame] | 1911 | .open = snd_usb_pcm_open, |
| 1912 | .close = snd_usb_pcm_close, |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1913 | .hw_params = snd_usb_hw_params, |
| 1914 | .hw_free = snd_usb_hw_free, |
| 1915 | .prepare = snd_usb_pcm_prepare, |
| 1916 | .trigger = snd_usb_substream_playback_trigger, |
Takashi Iwai | dc5eafe | 2019-12-10 07:34:54 +0100 | [diff] [blame] | 1917 | .sync_stop = snd_usb_pcm_sync_stop, |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1918 | .pointer = snd_usb_pcm_pointer, |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1919 | }; |
| 1920 | |
Arvind Yadav | 31cb1fb | 2017-08-18 13:15:21 +0530 | [diff] [blame] | 1921 | static const struct snd_pcm_ops snd_usb_capture_ops = { |
Takashi Iwai | 6fddc79 | 2018-05-27 13:59:03 +0200 | [diff] [blame] | 1922 | .open = snd_usb_pcm_open, |
| 1923 | .close = snd_usb_pcm_close, |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1924 | .hw_params = snd_usb_hw_params, |
| 1925 | .hw_free = snd_usb_hw_free, |
| 1926 | .prepare = snd_usb_pcm_prepare, |
| 1927 | .trigger = snd_usb_substream_capture_trigger, |
Takashi Iwai | dc5eafe | 2019-12-10 07:34:54 +0100 | [diff] [blame] | 1928 | .sync_stop = snd_usb_pcm_sync_stop, |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1929 | .pointer = snd_usb_pcm_pointer, |
Takashi Iwai | f274baa | 2018-05-27 13:01:17 +0200 | [diff] [blame] | 1930 | }; |
| 1931 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1932 | void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream) |
| 1933 | { |
Takashi Iwai | f274baa | 2018-05-27 13:01:17 +0200 | [diff] [blame] | 1934 | const struct snd_pcm_ops *ops; |
| 1935 | |
Takashi Iwai | b315997d | 2019-11-05 16:18:40 +0100 | [diff] [blame] | 1936 | ops = stream == SNDRV_PCM_STREAM_PLAYBACK ? |
Takashi Iwai | f274baa | 2018-05-27 13:01:17 +0200 | [diff] [blame] | 1937 | &snd_usb_playback_ops : &snd_usb_capture_ops; |
Takashi Iwai | f274baa | 2018-05-27 13:01:17 +0200 | [diff] [blame] | 1938 | snd_pcm_set_ops(pcm, stream, ops); |
| 1939 | } |
| 1940 | |
| 1941 | void snd_usb_preallocate_buffer(struct snd_usb_substream *subs) |
| 1942 | { |
| 1943 | struct snd_pcm *pcm = subs->stream->pcm; |
| 1944 | struct snd_pcm_substream *s = pcm->streams[subs->direction].substream; |
| 1945 | struct device *dev = subs->dev->bus->controller; |
| 1946 | |
Takashi Iwai | b315997d | 2019-11-05 16:18:40 +0100 | [diff] [blame] | 1947 | if (snd_usb_use_vmalloc) |
Takashi Iwai | 6dd9486ca | 2019-12-09 10:49:42 +0100 | [diff] [blame] | 1948 | snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_VMALLOC, |
| 1949 | NULL, 0, 0); |
Takashi Iwai | b315997d | 2019-11-05 16:18:40 +0100 | [diff] [blame] | 1950 | else |
Takashi Iwai | 6dd9486ca | 2019-12-09 10:49:42 +0100 | [diff] [blame] | 1951 | snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_DEV_SG, |
| 1952 | dev, 64*1024, 512*1024); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1953 | } |