blob: f61bf8421ed2412bc56e2e1eccfb17348cd0b16e [file] [log] [blame]
Daniel Macke5779992010-03-04 19:46:13 +01001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17#include <linux/init.h>
Stephen Rothwell9966dda2010-03-29 19:01:48 +110018#include <linux/slab.h>
Daniel Mack44dcbbb2013-04-17 00:01:39 +080019#include <linux/bitrev.h>
Daniel Mackedcd3632012-04-12 13:51:12 +020020#include <linux/ratelimit.h>
Daniel Macke5779992010-03-04 19:46:13 +010021#include <linux/usb.h>
22#include <linux/usb/audio.h>
Daniel Mack7e847892010-03-11 21:13:20 +010023#include <linux/usb/audio-v2.h>
Daniel Macke5779992010-03-04 19:46:13 +010024
25#include <sound/core.h>
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28
29#include "usbaudio.h"
30#include "card.h"
31#include "quirks.h"
32#include "debug.h"
Daniel Mackc731bc92011-09-14 12:46:57 +020033#include "endpoint.h"
Daniel Macke5779992010-03-04 19:46:13 +010034#include "helper.h"
35#include "pcm.h"
Daniel Mack79f920f2010-05-31 14:51:31 +020036#include "clock.h"
Oliver Neukum88a85162011-03-11 14:51:12 +010037#include "power.h"
Daniel Macke5779992010-03-04 19:46:13 +010038
Daniel Mackedcd3632012-04-12 13:51:12 +020039#define SUBSTREAM_FLAG_DATA_EP_STARTED 0
40#define SUBSTREAM_FLAG_SYNC_EP_STARTED 1
41
Hemant Kumard4101882017-08-18 18:44:43 -070042#define MAX_SETALT_TIMEOUT_MS 1000
43
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050044/* return the estimated delay based on USB frame counters */
45snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
46 unsigned int rate)
47{
48 int current_frame_number;
49 int frame_diff;
50 int est_delay;
51
Takashi Iwai48779a02012-11-23 16:00:37 +010052 if (!subs->last_delay)
53 return 0; /* short path */
54
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050055 current_frame_number = usb_get_current_frame_number(subs->dev);
56 /*
57 * HCD implementations use different widths, use lower 8 bits.
58 * The delay will be managed up to 256ms, which is more than
59 * enough
60 */
61 frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
62
63 /* Approximation based on number of samples per USB frame (ms),
64 some truncation for 44.1 but the estimate is good enough */
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -060065 est_delay = frame_diff * rate / 1000;
66 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
67 est_delay = subs->last_delay - est_delay;
68 else
69 est_delay = subs->last_delay + est_delay;
70
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050071 if (est_delay < 0)
72 est_delay = 0;
73 return est_delay;
74}
75
Daniel Macke5779992010-03-04 19:46:13 +010076/*
77 * return the current pcm pointer. just based on the hwptr_done value.
78 */
79static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
80{
81 struct snd_usb_substream *subs;
82 unsigned int hwptr_done;
83
84 subs = (struct snd_usb_substream *)substream->runtime->private_data;
Takashi Iwai47ab1542015-08-25 16:09:00 +020085 if (atomic_read(&subs->stream->chip->shutdown))
Takashi Iwai978520b2012-10-12 15:12:55 +020086 return SNDRV_PCM_POS_XRUN;
Daniel Macke5779992010-03-04 19:46:13 +010087 spin_lock(&subs->lock);
88 hwptr_done = subs->hwptr_done;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -060089 substream->runtime->delay = snd_usb_pcm_delay(subs,
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -050090 substream->runtime->rate);
Daniel Macke5779992010-03-04 19:46:13 +010091 spin_unlock(&subs->lock);
92 return hwptr_done / (substream->runtime->frame_bits >> 3);
93}
94
95/*
96 * find a matching audio format
97 */
Dylan Reid61a70952012-09-18 09:49:48 -070098static struct audioformat *find_format(struct snd_usb_substream *subs)
Daniel Macke5779992010-03-04 19:46:13 +010099{
Eldad Zack88766f02013-04-03 23:18:49 +0200100 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +0100101 struct audioformat *found = NULL;
102 int cur_attr = 0, attr;
103
Eldad Zack88766f02013-04-03 23:18:49 +0200104 list_for_each_entry(fp, &subs->fmt_list, list) {
Eldad Zack74c34ca2013-04-23 01:00:41 +0200105 if (!(fp->formats & pcm_format_to_bits(subs->pcm_format)))
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100106 continue;
Dylan Reid61a70952012-09-18 09:49:48 -0700107 if (fp->channels != subs->channels)
Daniel Macke5779992010-03-04 19:46:13 +0100108 continue;
Dylan Reid61a70952012-09-18 09:49:48 -0700109 if (subs->cur_rate < fp->rate_min ||
110 subs->cur_rate > fp->rate_max)
Daniel Macke5779992010-03-04 19:46:13 +0100111 continue;
112 if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
113 unsigned int i;
114 for (i = 0; i < fp->nr_rates; i++)
Dylan Reid61a70952012-09-18 09:49:48 -0700115 if (fp->rate_table[i] == subs->cur_rate)
Daniel Macke5779992010-03-04 19:46:13 +0100116 break;
117 if (i >= fp->nr_rates)
118 continue;
119 }
120 attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
121 if (! found) {
122 found = fp;
123 cur_attr = attr;
124 continue;
125 }
126 /* avoid async out and adaptive in if the other method
127 * supports the same format.
128 * this is a workaround for the case like
129 * M-audio audiophile USB.
130 */
131 if (attr != cur_attr) {
132 if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
133 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
134 (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
135 subs->direction == SNDRV_PCM_STREAM_CAPTURE))
136 continue;
137 if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
138 subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
139 (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
140 subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
141 found = fp;
142 cur_attr = attr;
143 continue;
144 }
145 }
146 /* find the format with the largest max. packet size */
147 if (fp->maxpacksize > found->maxpacksize) {
148 found = fp;
149 cur_attr = attr;
150 }
151 }
152 return found;
153}
154
Daniel Mack767d75a2010-03-04 19:46:17 +0100155static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
156 struct usb_host_interface *alts,
157 struct audioformat *fmt)
Daniel Macke5779992010-03-04 19:46:13 +0100158{
Daniel Mack767d75a2010-03-04 19:46:17 +0100159 struct usb_device *dev = chip->dev;
Daniel Macke5779992010-03-04 19:46:13 +0100160 unsigned int ep;
161 unsigned char data[1];
162 int err;
163
Takashi Iwai447d6272016-03-15 15:20:58 +0100164 if (get_iface_desc(alts)->bNumEndpoints < 1)
165 return -EINVAL;
Daniel Macke5779992010-03-04 19:46:13 +0100166 ep = get_endpoint(alts, 0)->bEndpointAddress;
Daniel Mack767d75a2010-03-04 19:46:17 +0100167
Daniel Mack767d75a2010-03-04 19:46:17 +0100168 data[0] = 1;
169 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
170 USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
171 UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200172 data, sizeof(data))) < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100173 usb_audio_err(chip, "%d:%d: cannot set enable PITCH\n",
174 iface, ep);
Daniel Mack767d75a2010-03-04 19:46:17 +0100175 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100176 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100177
Daniel Macke5779992010-03-04 19:46:13 +0100178 return 0;
179}
180
Daniel Mack92c25612010-05-26 18:11:39 +0200181static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
182 struct usb_host_interface *alts,
183 struct audioformat *fmt)
184{
185 struct usb_device *dev = chip->dev;
186 unsigned char data[1];
Daniel Mack92c25612010-05-26 18:11:39 +0200187 int err;
188
Daniel Mack92c25612010-05-26 18:11:39 +0200189 data[0] = 1;
190 if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
191 USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
192 UAC2_EP_CS_PITCH << 8, 0,
Clemens Ladisch17d900c2011-09-26 21:15:27 +0200193 data, sizeof(data))) < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100194 usb_audio_err(chip, "%d:%d: cannot set enable PITCH (v2)\n",
195 iface, fmt->altsetting);
Daniel Mack92c25612010-05-26 18:11:39 +0200196 return err;
197 }
198
199 return 0;
200}
201
Daniel Mack767d75a2010-03-04 19:46:17 +0100202/*
Daniel Mack92c25612010-05-26 18:11:39 +0200203 * initialize the pitch control and sample rate
Daniel Mack767d75a2010-03-04 19:46:17 +0100204 */
205int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
206 struct usb_host_interface *alts,
207 struct audioformat *fmt)
208{
Daniel Mack92c25612010-05-26 18:11:39 +0200209 /* if endpoint doesn't have pitch control, bail out */
210 if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
211 return 0;
212
Clemens Ladisch8f898e92013-01-31 21:39:17 +0100213 switch (fmt->protocol) {
Daniel Mack767d75a2010-03-04 19:46:17 +0100214 case UAC_VERSION_1:
Clemens Ladischa2acad82010-09-03 10:53:11 +0200215 default:
Daniel Mack767d75a2010-03-04 19:46:17 +0100216 return init_pitch_v1(chip, iface, alts, fmt);
217
218 case UAC_VERSION_2:
Daniel Mack92c25612010-05-26 18:11:39 +0200219 return init_pitch_v2(chip, iface, alts, fmt);
Daniel Mack767d75a2010-03-04 19:46:17 +0100220 }
Daniel Mack767d75a2010-03-04 19:46:17 +0100221}
222
Ioan-Adrian Ratiube4e3ae2017-01-05 00:37:46 +0200223static int start_endpoints(struct snd_usb_substream *subs)
Daniel Mackedcd3632012-04-12 13:51:12 +0200224{
225 int err;
226
227 if (!subs->data_endpoint)
228 return -EINVAL;
229
230 if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
231 struct snd_usb_endpoint *ep = subs->data_endpoint;
232
Vamsi Krishna Samavedam0836aae2016-11-03 17:21:02 -0700233 dev_dbg(&subs->dev->dev, "Starting data EP @%pK\n", ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200234
235 ep->data_subs = subs;
Ioan-Adrian Ratiube4e3ae2017-01-05 00:37:46 +0200236 err = snd_usb_endpoint_start(ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200237 if (err < 0) {
238 clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
239 return err;
240 }
241 }
242
243 if (subs->sync_endpoint &&
244 !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
245 struct snd_usb_endpoint *ep = subs->sync_endpoint;
246
Daniel Mack2e4a2632012-08-30 18:52:31 +0200247 if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
Eldad Zackdf23a242013-10-06 22:31:13 +0200248 subs->data_endpoint->altsetting != subs->sync_endpoint->altsetting) {
Daniel Mack2e4a2632012-08-30 18:52:31 +0200249 err = usb_set_interface(subs->dev,
250 subs->sync_endpoint->iface,
Eldad Zackdf23a242013-10-06 22:31:13 +0200251 subs->sync_endpoint->altsetting);
Daniel Mack2e4a2632012-08-30 18:52:31 +0200252 if (err < 0) {
Eldad Zack06613f52013-10-06 22:31:11 +0200253 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100254 dev_err(&subs->dev->dev,
255 "%d:%d: cannot set interface (%d)\n",
Daniel Mack2e4a2632012-08-30 18:52:31 +0200256 subs->sync_endpoint->iface,
Eldad Zackdf23a242013-10-06 22:31:13 +0200257 subs->sync_endpoint->altsetting, err);
Daniel Mack2e4a2632012-08-30 18:52:31 +0200258 return -EIO;
259 }
260 }
261
Vamsi Krishna Samavedam0836aae2016-11-03 17:21:02 -0700262 dev_dbg(&subs->dev->dev, "Starting sync EP @%pK\n", ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200263
264 ep->sync_slave = subs->data_endpoint;
Ioan-Adrian Ratiube4e3ae2017-01-05 00:37:46 +0200265 err = snd_usb_endpoint_start(ep);
Daniel Mackedcd3632012-04-12 13:51:12 +0200266 if (err < 0) {
267 clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
268 return err;
269 }
270 }
271
272 return 0;
273}
274
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100275static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
Daniel Mackedcd3632012-04-12 13:51:12 +0200276{
277 if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100278 snd_usb_endpoint_stop(subs->sync_endpoint);
Daniel Mackedcd3632012-04-12 13:51:12 +0200279
280 if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
Takashi Iwaib2eb9502012-11-21 08:30:48 +0100281 snd_usb_endpoint_stop(subs->data_endpoint);
282
283 if (wait) {
284 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
285 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
286 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200287}
288
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100289static int search_roland_implicit_fb(struct usb_device *dev, int ifnum,
290 unsigned int altsetting,
291 struct usb_host_interface **alts,
292 unsigned int *ep)
293{
294 struct usb_interface *iface;
295 struct usb_interface_descriptor *altsd;
296 struct usb_endpoint_descriptor *epd;
297
298 iface = usb_ifnum_to_if(dev, ifnum);
299 if (!iface || iface->num_altsetting < altsetting + 1)
300 return -ENOENT;
301 *alts = &iface->altsetting[altsetting];
302 altsd = get_iface_desc(*alts);
303 if (altsd->bAlternateSetting != altsetting ||
304 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
305 (altsd->bInterfaceSubClass != 2 &&
306 altsd->bInterfaceProtocol != 2 ) ||
307 altsd->bNumEndpoints < 1)
308 return -ENOENT;
309 epd = get_endpoint(*alts, 0);
310 if (!usb_endpoint_is_isoc_in(epd) ||
311 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
312 USB_ENDPOINT_USAGE_IMPLICIT_FB)
313 return -ENOENT;
314 *ep = epd->bEndpointAddress;
315 return 0;
316}
317
Manuel Reinhardtc504b842019-01-31 15:32:35 +0100318/* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk
319 * applies. Returns 1 if a quirk was found.
320 */
Eldad Zacka60945f2013-08-03 10:50:18 +0200321static int set_sync_ep_implicit_fb_quirk(struct snd_usb_substream *subs,
322 struct usb_device *dev,
323 struct usb_interface_descriptor *altsd,
324 unsigned int attr)
Daniel Macke5779992010-03-04 19:46:13 +0100325{
Eldad Zacka60945f2013-08-03 10:50:18 +0200326 struct usb_host_interface *alts;
Daniel Macke5779992010-03-04 19:46:13 +0100327 struct usb_interface *iface;
Eldad Zacka60945f2013-08-03 10:50:18 +0200328 unsigned int ep;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200329
Eldad Zack914273c2013-08-03 10:50:21 +0200330 /* Implicit feedback sync EPs consumers are always playback EPs */
331 if (subs->direction != SNDRV_PCM_STREAM_PLAYBACK)
332 return 0;
333
Daniel Mackc75a8a72012-04-12 13:51:14 +0200334 switch (subs->stream->chip->usb_id) {
Eldad Zackca10a7e2012-11-28 23:55:41 +0100335 case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
Matt Gruskine9a25e02013-02-09 12:56:35 -0500336 case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
Eldad Zack914273c2013-08-03 10:50:21 +0200337 ep = 0x81;
338 iface = usb_ifnum_to_if(dev, 3);
Eldad Zackca10a7e2012-11-28 23:55:41 +0100339
Eldad Zack914273c2013-08-03 10:50:21 +0200340 if (!iface || iface->num_altsetting == 0)
341 return -EINVAL;
Eldad Zackca10a7e2012-11-28 23:55:41 +0100342
Eldad Zack914273c2013-08-03 10:50:21 +0200343 alts = &iface->altsetting[1];
344 goto add_sync_ep;
Eldad Zackca10a7e2012-11-28 23:55:41 +0100345 break;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200346 case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
347 case USB_ID(0x0763, 0x2081):
Eldad Zack914273c2013-08-03 10:50:21 +0200348 ep = 0x81;
349 iface = usb_ifnum_to_if(dev, 2);
Daniel Mackc75a8a72012-04-12 13:51:14 +0200350
Eldad Zack914273c2013-08-03 10:50:21 +0200351 if (!iface || iface->num_altsetting == 0)
352 return -EINVAL;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200353
Eldad Zack914273c2013-08-03 10:50:21 +0200354 alts = &iface->altsetting[1];
355 goto add_sync_ep;
Alberto Aguirrec0aac1b2017-04-04 19:32:13 +0000356 case USB_ID(0x2466, 0x8003):
357 ep = 0x86;
358 iface = usb_ifnum_to_if(dev, 2);
359
360 if (!iface || iface->num_altsetting == 0)
361 return -EINVAL;
362
363 alts = &iface->altsetting[1];
364 goto add_sync_ep;
Lassi Ylikojola344c9ac2018-02-09 16:51:36 +0200365 case USB_ID(0x1397, 0x0002):
366 ep = 0x81;
367 iface = usb_ifnum_to_if(dev, 1);
368
369 if (!iface || iface->num_altsetting == 0)
370 return -EINVAL;
371
372 alts = &iface->altsetting[1];
373 goto add_sync_ep;
Alberto Aguirrec0aac1b2017-04-04 19:32:13 +0000374
Daniel Mackc75a8a72012-04-12 13:51:14 +0200375 }
Eldad Zack914273c2013-08-03 10:50:21 +0200376 if (attr == USB_ENDPOINT_SYNC_ASYNC &&
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100377 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
378 altsd->bInterfaceProtocol == 2 &&
379 altsd->bNumEndpoints == 1 &&
380 USB_ID_VENDOR(subs->stream->chip->usb_id) == 0x0582 /* Roland */ &&
381 search_roland_implicit_fb(dev, altsd->bInterfaceNumber + 1,
382 altsd->bAlternateSetting,
383 &alts, &ep) >= 0) {
Clemens Ladischba7c2be2013-02-03 22:31:20 +0100384 goto add_sync_ep;
385 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200386
Eldad Zacka60945f2013-08-03 10:50:18 +0200387 /* No quirk */
388 return 0;
389
390add_sync_ep:
391 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
392 alts, ep, !subs->direction,
Eldad Zack88abb8e2013-08-03 10:51:14 +0200393 SND_USB_ENDPOINT_TYPE_DATA);
Eldad Zacka60945f2013-08-03 10:50:18 +0200394 if (!subs->sync_endpoint)
395 return -EINVAL;
396
397 subs->data_endpoint->sync_master = subs->sync_endpoint;
398
Manuel Reinhardtc504b842019-01-31 15:32:35 +0100399 return 1;
Eldad Zacka60945f2013-08-03 10:50:18 +0200400}
401
402static int set_sync_endpoint(struct snd_usb_substream *subs,
403 struct audioformat *fmt,
404 struct usb_device *dev,
405 struct usb_host_interface *alts,
406 struct usb_interface_descriptor *altsd)
407{
408 int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
409 unsigned int ep, attr;
Eldad Zack95fec882013-08-03 10:50:20 +0200410 bool implicit_fb;
Eldad Zacka60945f2013-08-03 10:50:18 +0200411 int err;
412
413 /* we need a sync pipe in async OUT or adaptive IN mode */
414 /* check the number of EP, since some devices have broken
415 * descriptors which fool us. if it has only one EP,
416 * assume it as adaptive-out or sync-in.
417 */
418 attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
419
Pierre-Louis Bossart63018442015-08-14 17:19:42 -0500420 if ((is_playback && (attr != USB_ENDPOINT_SYNC_ASYNC)) ||
421 (!is_playback && (attr != USB_ENDPOINT_SYNC_ADAPTIVE))) {
422
423 /*
424 * In these modes the notion of sync_endpoint is irrelevant.
425 * Reset pointers to avoid using stale data from previously
426 * used settings, e.g. when configuration and endpoints were
427 * changed
428 */
429
430 subs->sync_endpoint = NULL;
431 subs->data_endpoint->sync_master = NULL;
432 }
433
Eldad Zacka60945f2013-08-03 10:50:18 +0200434 err = set_sync_ep_implicit_fb_quirk(subs, dev, altsd, attr);
435 if (err < 0)
436 return err;
437
Manuel Reinhardtc504b842019-01-31 15:32:35 +0100438 /* endpoint set by quirk */
439 if (err > 0)
440 return 0;
441
Eldad Zackf34d0652013-08-03 10:50:19 +0200442 if (altsd->bNumEndpoints < 2)
443 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200444
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500445 if ((is_playback && (attr == USB_ENDPOINT_SYNC_SYNC ||
446 attr == USB_ENDPOINT_SYNC_ADAPTIVE)) ||
Eldad Zackf34d0652013-08-03 10:50:19 +0200447 (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
448 return 0;
Daniel Mackc75a8a72012-04-12 13:51:14 +0200449
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500450 /*
451 * In case of illegal SYNC_NONE for OUT endpoint, we keep going to see
452 * if we don't find a sync endpoint, as on M-Audio Transit. In case of
453 * error fall back to SYNC mode and don't create sync endpoint
454 */
455
Eldad Zackf34d0652013-08-03 10:50:19 +0200456 /* check sync-pipe endpoint */
457 /* ... and check descriptor size before accessing bSynchAddress
458 because there is a version of the SB Audigy 2 NX firmware lacking
459 the audio fields in the endpoint descriptors */
460 if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
461 (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Eldad Zack95fec882013-08-03 10:50:20 +0200462 get_endpoint(alts, 1)->bSynchAddress != 0)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100463 dev_err(&dev->dev,
464 "%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
465 fmt->iface, fmt->altsetting,
Eldad Zackf34d0652013-08-03 10:50:19 +0200466 get_endpoint(alts, 1)->bmAttributes,
467 get_endpoint(alts, 1)->bLength,
468 get_endpoint(alts, 1)->bSynchAddress);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500469 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
470 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200471 return -EINVAL;
Daniel Mackedcd3632012-04-12 13:51:12 +0200472 }
Eldad Zackf34d0652013-08-03 10:50:19 +0200473 ep = get_endpoint(alts, 1)->bEndpointAddress;
Eldad Zack95fec882013-08-03 10:50:20 +0200474 if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
Eldad Zackf34d0652013-08-03 10:50:19 +0200475 ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
476 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100477 dev_err(&dev->dev,
478 "%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
479 fmt->iface, fmt->altsetting,
Eldad Zackf34d0652013-08-03 10:50:19 +0200480 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500481 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
482 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200483 return -EINVAL;
484 }
485
486 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
487 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
488
489 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
490 alts, ep, !subs->direction,
491 implicit_fb ?
492 SND_USB_ENDPOINT_TYPE_DATA :
493 SND_USB_ENDPOINT_TYPE_SYNC);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500494 if (!subs->sync_endpoint) {
495 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
496 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200497 return -EINVAL;
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500498 }
Eldad Zackf34d0652013-08-03 10:50:19 +0200499
500 subs->data_endpoint->sync_master = subs->sync_endpoint;
Daniel Macke5779992010-03-04 19:46:13 +0100501
Eldad Zack71bb64c2013-08-03 10:50:17 +0200502 return 0;
503}
504
505/*
506 * find a matching format and set up the interface
507 */
508static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
509{
510 struct usb_device *dev = subs->dev;
511 struct usb_host_interface *alts;
512 struct usb_interface_descriptor *altsd;
513 struct usb_interface *iface;
514 int err;
515
516 iface = usb_ifnum_to_if(dev, fmt->iface);
517 if (WARN_ON(!iface))
518 return -EINVAL;
519 alts = &iface->altsetting[fmt->altset_idx];
520 altsd = get_iface_desc(alts);
521 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
522 return -EINVAL;
523
524 if (fmt == subs->cur_audiofmt)
525 return 0;
526
527 /* close the old interface */
528 if (subs->interface >= 0 && subs->interface != fmt->iface) {
Hemant Kumard4101882017-08-18 18:44:43 -0700529 err = usb_set_interface_timeout(subs->dev, subs->interface, 0,
530 MAX_SETALT_TIMEOUT_MS);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200531 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100532 dev_err(&dev->dev,
533 "%d:%d: return to setting 0 failed (%d)\n",
534 fmt->iface, fmt->altsetting, err);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200535 return -EIO;
536 }
537 subs->interface = -1;
538 subs->altset_idx = 0;
539 }
540
541 /* set interface */
542 if (subs->interface != fmt->iface ||
543 subs->altset_idx != fmt->altset_idx) {
Jurgen Kramer6874daa2014-11-28 17:32:54 +0100544
545 err = snd_usb_select_mode_quirk(subs, fmt);
546 if (err < 0)
547 return -EIO;
548
Hemant Kumard4101882017-08-18 18:44:43 -0700549 err = usb_set_interface_timeout(dev, fmt->iface,
550 fmt->altsetting, MAX_SETALT_TIMEOUT_MS);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200551 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100552 dev_err(&dev->dev,
553 "%d:%d: usb_set_interface failed (%d)\n",
554 fmt->iface, fmt->altsetting, err);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200555 return -EIO;
556 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100557 dev_dbg(&dev->dev, "setting usb interface %d:%d\n",
558 fmt->iface, fmt->altsetting);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200559 subs->interface = fmt->iface;
560 subs->altset_idx = fmt->altset_idx;
561
562 snd_usb_set_interface_quirk(dev);
563 }
564
565 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
566 alts, fmt->endpoint, subs->direction,
567 SND_USB_ENDPOINT_TYPE_DATA);
568
569 if (!subs->data_endpoint)
570 return -EINVAL;
571
572 err = set_sync_endpoint(subs, fmt, dev, alts, altsd);
573 if (err < 0)
574 return err;
575
Eldad Zackd133f2c2013-08-03 10:50:16 +0200576 err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
577 if (err < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100578 return err;
579
580 subs->cur_audiofmt = fmt;
581
582 snd_usb_set_format_quirk(subs, fmt);
583
Daniel Macke5779992010-03-04 19:46:13 +0100584 return 0;
585}
586
Hemant Kumar9d9dac12016-03-07 14:46:31 -0800587int snd_usb_enable_audio_stream(struct snd_usb_substream *subs,
588 bool enable)
589{
590 struct audioformat *fmt;
591 struct usb_host_interface *alts;
592 struct usb_interface *iface;
593 int ret;
594
595 if (!enable) {
596 if (subs->interface >= 0) {
Hemant Kumard4101882017-08-18 18:44:43 -0700597 usb_set_interface_timeout(subs->dev, subs->interface, 0,
598 MAX_SETALT_TIMEOUT_MS);
Hemant Kumar9d9dac12016-03-07 14:46:31 -0800599 subs->altset_idx = 0;
600 subs->interface = -1;
601 subs->cur_audiofmt = NULL;
602 }
603
604 snd_usb_autosuspend(subs->stream->chip);
605 return 0;
606 }
607
608 snd_usb_autoresume(subs->stream->chip);
609 fmt = find_format(subs);
610 if (!fmt) {
Hemant Kumar43041092016-08-16 15:17:08 -0700611 dev_err(&subs->dev->dev,
Hemant Kumar9d9dac12016-03-07 14:46:31 -0800612 "cannot set format: format = %#x, rate = %d, channels = %d\n",
613 subs->pcm_format, subs->cur_rate, subs->channels);
614 return -EINVAL;
615 }
616
617 subs->altset_idx = 0;
618 subs->interface = -1;
619 if (atomic_read(&subs->stream->chip->shutdown)) {
620 ret = -ENODEV;
621 } else {
622 ret = set_format(subs, fmt);
623 if (ret < 0)
624 return ret;
625
626 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
627 if (!iface) {
628 dev_err(&subs->dev->dev, "Could not get iface %d\n",
629 subs->cur_audiofmt->iface);
630 return -ENODEV;
631 }
632
633 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
634 ret = snd_usb_init_sample_rate(subs->stream->chip,
635 subs->cur_audiofmt->iface,
636 alts,
637 subs->cur_audiofmt,
638 subs->cur_rate);
639 if (ret < 0) {
640 dev_err(&subs->dev->dev, "failed to set rate %d\n",
641 subs->cur_rate);
642 return ret;
643 }
644 }
645
646 subs->interface = fmt->iface;
647 subs->altset_idx = fmt->altset_idx;
648
649 return 0;
650}
651
Daniel Macke5779992010-03-04 19:46:13 +0100652/*
Eldad Zack0d9741c2012-12-03 20:30:09 +0100653 * Return the score of matching two audioformats.
654 * Veto the audioformat if:
655 * - It has no channels for some reason.
656 * - Requested PCM format is not supported.
657 * - Requested sample rate is not supported.
658 */
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100659static int match_endpoint_audioformats(struct snd_usb_substream *subs,
660 struct audioformat *fp,
661 struct audioformat *match, int rate,
662 snd_pcm_format_t pcm_format)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100663{
664 int i;
665 int score = 0;
666
667 if (fp->channels < 1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100668 dev_dbg(&subs->dev->dev,
Vamsi Krishna Samavedam0836aae2016-11-03 17:21:02 -0700669 "%s: (fmt @%pK) no channels\n", __func__, fp);
Eldad Zack0d9741c2012-12-03 20:30:09 +0100670 return 0;
671 }
672
Eldad Zack74c34ca2013-04-23 01:00:41 +0200673 if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100674 dev_dbg(&subs->dev->dev,
Vamsi Krishna Samavedam0836aae2016-11-03 17:21:02 -0700675 "%s: (fmt @%pK) no match for format %d\n", __func__,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100676 fp, pcm_format);
677 return 0;
678 }
679
680 for (i = 0; i < fp->nr_rates; i++) {
681 if (fp->rate_table[i] == rate) {
682 score++;
683 break;
684 }
685 }
686 if (!score) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100687 dev_dbg(&subs->dev->dev,
Vamsi Krishna Samavedam0836aae2016-11-03 17:21:02 -0700688 "%s: (fmt @%pK) no match for rate %d\n", __func__,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100689 fp, rate);
690 return 0;
691 }
692
693 if (fp->channels == match->channels)
694 score++;
695
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100696 dev_dbg(&subs->dev->dev,
Vamsi Krishna Samavedam0836aae2016-11-03 17:21:02 -0700697 "%s: (fmt @%pK) score %d\n", __func__, fp, score);
Eldad Zack0d9741c2012-12-03 20:30:09 +0100698
699 return score;
700}
701
702/*
703 * Configure the sync ep using the rate and pcm format of the data ep.
704 */
705static int configure_sync_endpoint(struct snd_usb_substream *subs)
706{
707 int ret;
708 struct audioformat *fp;
709 struct audioformat *sync_fp = NULL;
710 int cur_score = 0;
711 int sync_period_bytes = subs->period_bytes;
712 struct snd_usb_substream *sync_subs =
713 &subs->stream->substream[subs->direction ^ 1];
714
Takashi Iwai31be5422013-01-10 14:06:38 +0100715 if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
716 !subs->stream)
717 return snd_usb_endpoint_set_params(subs->sync_endpoint,
718 subs->pcm_format,
719 subs->channels,
720 subs->period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400721 0, 0,
Takashi Iwai31be5422013-01-10 14:06:38 +0100722 subs->cur_rate,
723 subs->cur_audiofmt,
724 NULL);
725
Eldad Zack0d9741c2012-12-03 20:30:09 +0100726 /* Try to find the best matching audioformat. */
727 list_for_each_entry(fp, &sync_subs->fmt_list, list) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100728 int score = match_endpoint_audioformats(subs,
729 fp, subs->cur_audiofmt,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100730 subs->cur_rate, subs->pcm_format);
731
732 if (score > cur_score) {
733 sync_fp = fp;
734 cur_score = score;
735 }
736 }
737
738 if (unlikely(sync_fp == NULL)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100739 dev_err(&subs->dev->dev,
740 "%s: no valid audioformat for sync ep %x found\n",
Eldad Zack0d9741c2012-12-03 20:30:09 +0100741 __func__, sync_subs->ep_num);
742 return -EINVAL;
743 }
744
745 /*
746 * Recalculate the period bytes if channel number differ between
747 * data and sync ep audioformat.
748 */
749 if (sync_fp->channels != subs->channels) {
750 sync_period_bytes = (subs->period_bytes / subs->channels) *
751 sync_fp->channels;
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100752 dev_dbg(&subs->dev->dev,
753 "%s: adjusted sync ep period bytes (%d -> %d)\n",
Eldad Zack0d9741c2012-12-03 20:30:09 +0100754 __func__, subs->period_bytes, sync_period_bytes);
755 }
756
757 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
758 subs->pcm_format,
759 sync_fp->channels,
760 sync_period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400761 0, 0,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100762 subs->cur_rate,
763 sync_fp,
764 NULL);
765
766 return ret;
767}
768
769/*
Dylan Reid61a70952012-09-18 09:49:48 -0700770 * configure endpoint params
771 *
772 * called during initial setup and upon resume
773 */
774static int configure_endpoint(struct snd_usb_substream *subs)
775{
776 int ret;
777
Dylan Reid61a70952012-09-18 09:49:48 -0700778 /* format changed */
Takashi Iwaib0db6062012-11-21 08:35:42 +0100779 stop_endpoints(subs, true);
Dylan Reid61a70952012-09-18 09:49:48 -0700780 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
781 subs->pcm_format,
782 subs->channels,
783 subs->period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400784 subs->period_frames,
785 subs->buffer_periods,
Dylan Reid61a70952012-09-18 09:49:48 -0700786 subs->cur_rate,
787 subs->cur_audiofmt,
788 subs->sync_endpoint);
789 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200790 return ret;
Dylan Reid61a70952012-09-18 09:49:48 -0700791
792 if (subs->sync_endpoint)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100793 ret = configure_sync_endpoint(subs);
794
Dylan Reid61a70952012-09-18 09:49:48 -0700795 return ret;
796}
797
798/*
Daniel Macke5779992010-03-04 19:46:13 +0100799 * hw_params callback
800 *
801 * allocate a buffer and set the given audio format.
802 *
803 * so far we use a physically linear buffer although packetize transfer
804 * doesn't need a continuous area.
805 * if sg buffer is supported on the later version of alsa, we'll follow
806 * that.
807 */
808static int snd_usb_hw_params(struct snd_pcm_substream *substream,
809 struct snd_pcm_hw_params *hw_params)
810{
811 struct snd_usb_substream *subs = substream->runtime->private_data;
812 struct audioformat *fmt;
Dylan Reid61a70952012-09-18 09:49:48 -0700813 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100814
815 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
816 params_buffer_bytes(hw_params));
817 if (ret < 0)
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -0300818 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100819
Dylan Reid61a70952012-09-18 09:49:48 -0700820 subs->pcm_format = params_format(hw_params);
821 subs->period_bytes = params_period_bytes(hw_params);
Alan Stern976b6c02013-09-24 15:51:58 -0400822 subs->period_frames = params_period_size(hw_params);
823 subs->buffer_periods = params_periods(hw_params);
Dylan Reid61a70952012-09-18 09:49:48 -0700824 subs->channels = params_channels(hw_params);
825 subs->cur_rate = params_rate(hw_params);
826
827 fmt = find_format(subs);
Daniel Macke5779992010-03-04 19:46:13 +0100828 if (!fmt) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100829 dev_dbg(&subs->dev->dev,
830 "cannot set format: format = %#x, rate = %d, channels = %d\n",
Dylan Reid61a70952012-09-18 09:49:48 -0700831 subs->pcm_format, subs->cur_rate, subs->channels);
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -0300832 return -EINVAL;
Daniel Macke5779992010-03-04 19:46:13 +0100833 }
834
Takashi Iwai47ab1542015-08-25 16:09:00 +0200835 ret = snd_usb_lock_shutdown(subs->stream->chip);
836 if (ret < 0)
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -0300837 return ret;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200838 ret = set_format(subs, fmt);
839 snd_usb_unlock_shutdown(subs->stream->chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200840 if (ret < 0)
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -0300841 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100842
Dylan Reid61a70952012-09-18 09:49:48 -0700843 subs->interface = fmt->iface;
844 subs->altset_idx = fmt->altset_idx;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200845 subs->need_setup_ep = true;
Daniel Macke5779992010-03-04 19:46:13 +0100846
Dylan Reid61a70952012-09-18 09:49:48 -0700847 return 0;
Daniel Macke5779992010-03-04 19:46:13 +0100848}
849
850/*
851 * hw_free callback
852 *
853 * reset the audio format and release the buffer
854 */
855static int snd_usb_hw_free(struct snd_pcm_substream *substream)
856{
857 struct snd_usb_substream *subs = substream->runtime->private_data;
858
859 subs->cur_audiofmt = NULL;
860 subs->cur_rate = 0;
861 subs->period_bytes = 0;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200862 if (!snd_usb_lock_shutdown(subs->stream->chip)) {
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100863 stop_endpoints(subs, true);
Eldad Zack26de5d02013-10-06 22:31:07 +0200864 snd_usb_endpoint_deactivate(subs->sync_endpoint);
865 snd_usb_endpoint_deactivate(subs->data_endpoint);
Takashi Iwai47ab1542015-08-25 16:09:00 +0200866 snd_usb_unlock_shutdown(subs->stream->chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200867 }
Daniel Macke5779992010-03-04 19:46:13 +0100868 return snd_pcm_lib_free_vmalloc_buffer(substream);
869}
870
871/*
872 * prepare callback
873 *
874 * only a few subtle things...
875 */
876static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
877{
878 struct snd_pcm_runtime *runtime = substream->runtime;
879 struct snd_usb_substream *subs = runtime->private_data;
Dylan Reid61a70952012-09-18 09:49:48 -0700880 struct usb_host_interface *alts;
881 struct usb_interface *iface;
882 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100883
884 if (! subs->cur_audiofmt) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100885 dev_err(&subs->dev->dev, "no format is specified!\n");
Daniel Macke5779992010-03-04 19:46:13 +0100886 return -ENXIO;
887 }
888
Takashi Iwai47ab1542015-08-25 16:09:00 +0200889 ret = snd_usb_lock_shutdown(subs->stream->chip);
890 if (ret < 0)
891 return ret;
Takashi Iwai978520b2012-10-12 15:12:55 +0200892 if (snd_BUG_ON(!subs->data_endpoint)) {
893 ret = -EIO;
894 goto unlock;
895 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200896
Takashi Iwaif58161b2012-11-08 08:52:45 +0100897 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
898 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
899
Dylan Reid61a70952012-09-18 09:49:48 -0700900 ret = set_format(subs, subs->cur_audiofmt);
901 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200902 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700903
904 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
905 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
906 ret = snd_usb_init_sample_rate(subs->stream->chip,
907 subs->cur_audiofmt->iface,
908 alts,
909 subs->cur_audiofmt,
910 subs->cur_rate);
911 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200912 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700913
Takashi Iwai384dc0852012-09-18 14:49:31 +0200914 if (subs->need_setup_ep) {
915 ret = configure_endpoint(subs);
916 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200917 goto unlock;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200918 subs->need_setup_ep = false;
919 }
Dylan Reid61a70952012-09-18 09:49:48 -0700920
Daniel Macke5779992010-03-04 19:46:13 +0100921 /* some unit conversions in runtime */
Daniel Mackedcd3632012-04-12 13:51:12 +0200922 subs->data_endpoint->maxframesize =
923 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
924 subs->data_endpoint->curframesize =
925 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
Daniel Macke5779992010-03-04 19:46:13 +0100926
927 /* reset the pointer */
928 subs->hwptr_done = 0;
929 subs->transfer_done = 0;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -0500930 subs->last_delay = 0;
931 subs->last_frame_number = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100932 runtime->delay = 0;
933
Daniel Mackedcd3632012-04-12 13:51:12 +0200934 /* for playback, submit the URBs now; otherwise, the first hwptr_done
935 * updates for all URBs would happen at the same time when starting */
936 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
Ioan-Adrian Ratiube4e3ae2017-01-05 00:37:46 +0200937 ret = start_endpoints(subs);
Daniel Mackedcd3632012-04-12 13:51:12 +0200938
Takashi Iwai978520b2012-10-12 15:12:55 +0200939 unlock:
Takashi Iwai47ab1542015-08-25 16:09:00 +0200940 snd_usb_unlock_shutdown(subs->stream->chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200941 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100942}
943
944static struct snd_pcm_hardware snd_usb_hardware =
945{
946 .info = SNDRV_PCM_INFO_MMAP |
947 SNDRV_PCM_INFO_MMAP_VALID |
948 SNDRV_PCM_INFO_BATCH |
949 SNDRV_PCM_INFO_INTERLEAVED |
950 SNDRV_PCM_INFO_BLOCK_TRANSFER |
951 SNDRV_PCM_INFO_PAUSE,
952 .buffer_bytes_max = 1024 * 1024,
953 .period_bytes_min = 64,
954 .period_bytes_max = 512 * 1024,
955 .periods_min = 2,
956 .periods_max = 1024,
957};
958
959static int hw_check_valid_format(struct snd_usb_substream *subs,
960 struct snd_pcm_hw_params *params,
961 struct audioformat *fp)
962{
963 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
964 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
965 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
966 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100967 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +0100968 unsigned int ptime;
969
970 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100971 snd_mask_none(&check_fmts);
972 check_fmts.bits[0] = (u32)fp->formats;
973 check_fmts.bits[1] = (u32)(fp->formats >> 32);
974 snd_mask_intersect(&check_fmts, fmts);
975 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +0100976 hwc_debug(" > check: no supported format %d\n", fp->format);
977 return 0;
978 }
979 /* check the channels */
980 if (fp->channels < ct->min || fp->channels > ct->max) {
981 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
982 return 0;
983 }
984 /* check the rate is within the range */
985 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
986 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
987 return 0;
988 }
989 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
990 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
991 return 0;
992 }
993 /* check whether the period time is >= the data packet interval */
Takashi Iwai978520b2012-10-12 15:12:55 +0200994 if (subs->speed != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +0100995 ptime = 125 * (1 << fp->datainterval);
996 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
997 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
998 return 0;
999 }
1000 }
1001 return 1;
1002}
1003
1004static int hw_rule_rate(struct snd_pcm_hw_params *params,
1005 struct snd_pcm_hw_rule *rule)
1006{
1007 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +02001008 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001009 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1010 unsigned int rmin, rmax;
1011 int changed;
1012
1013 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
1014 changed = 0;
1015 rmin = rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001016 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001017 if (!hw_check_valid_format(subs, params, fp))
1018 continue;
1019 if (changed++) {
1020 if (rmin > fp->rate_min)
1021 rmin = fp->rate_min;
1022 if (rmax < fp->rate_max)
1023 rmax = fp->rate_max;
1024 } else {
1025 rmin = fp->rate_min;
1026 rmax = fp->rate_max;
1027 }
1028 }
1029
1030 if (!changed) {
1031 hwc_debug(" --> get empty\n");
1032 it->empty = 1;
1033 return -EINVAL;
1034 }
1035
1036 changed = 0;
1037 if (it->min < rmin) {
1038 it->min = rmin;
1039 it->openmin = 0;
1040 changed = 1;
1041 }
1042 if (it->max > rmax) {
1043 it->max = rmax;
1044 it->openmax = 0;
1045 changed = 1;
1046 }
1047 if (snd_interval_checkempty(it)) {
1048 it->empty = 1;
1049 return -EINVAL;
1050 }
1051 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1052 return changed;
1053}
1054
1055
1056static int hw_rule_channels(struct snd_pcm_hw_params *params,
1057 struct snd_pcm_hw_rule *rule)
1058{
1059 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +02001060 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001061 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1062 unsigned int rmin, rmax;
1063 int changed;
1064
1065 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
1066 changed = 0;
1067 rmin = rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001068 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001069 if (!hw_check_valid_format(subs, params, fp))
1070 continue;
1071 if (changed++) {
1072 if (rmin > fp->channels)
1073 rmin = fp->channels;
1074 if (rmax < fp->channels)
1075 rmax = fp->channels;
1076 } else {
1077 rmin = fp->channels;
1078 rmax = fp->channels;
1079 }
1080 }
1081
1082 if (!changed) {
1083 hwc_debug(" --> get empty\n");
1084 it->empty = 1;
1085 return -EINVAL;
1086 }
1087
1088 changed = 0;
1089 if (it->min < rmin) {
1090 it->min = rmin;
1091 it->openmin = 0;
1092 changed = 1;
1093 }
1094 if (it->max > rmax) {
1095 it->max = rmax;
1096 it->openmax = 0;
1097 changed = 1;
1098 }
1099 if (snd_interval_checkempty(it)) {
1100 it->empty = 1;
1101 return -EINVAL;
1102 }
1103 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1104 return changed;
1105}
1106
1107static int hw_rule_format(struct snd_pcm_hw_params *params,
1108 struct snd_pcm_hw_rule *rule)
1109{
1110 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +02001111 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001112 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1113 u64 fbits;
1114 u32 oldbits[2];
1115 int changed;
1116
1117 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1118 fbits = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001119 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001120 if (!hw_check_valid_format(subs, params, fp))
1121 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +01001122 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +01001123 }
1124
1125 oldbits[0] = fmt->bits[0];
1126 oldbits[1] = fmt->bits[1];
1127 fmt->bits[0] &= (u32)fbits;
1128 fmt->bits[1] &= (u32)(fbits >> 32);
1129 if (!fmt->bits[0] && !fmt->bits[1]) {
1130 hwc_debug(" --> get empty\n");
1131 return -EINVAL;
1132 }
1133 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1134 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1135 return changed;
1136}
1137
1138static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1139 struct snd_pcm_hw_rule *rule)
1140{
1141 struct snd_usb_substream *subs = rule->private;
1142 struct audioformat *fp;
1143 struct snd_interval *it;
1144 unsigned char min_datainterval;
1145 unsigned int pmin;
1146 int changed;
1147
1148 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1149 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1150 min_datainterval = 0xff;
1151 list_for_each_entry(fp, &subs->fmt_list, list) {
1152 if (!hw_check_valid_format(subs, params, fp))
1153 continue;
1154 min_datainterval = min(min_datainterval, fp->datainterval);
1155 }
1156 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +02001157 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +01001158 it->empty = 1;
1159 return -EINVAL;
1160 }
1161 pmin = 125 * (1 << min_datainterval);
1162 changed = 0;
1163 if (it->min < pmin) {
1164 it->min = pmin;
1165 it->openmin = 0;
1166 changed = 1;
1167 }
1168 if (snd_interval_checkempty(it)) {
1169 it->empty = 1;
1170 return -EINVAL;
1171 }
1172 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
1173 return changed;
1174}
1175
1176/*
1177 * If the device supports unusual bit rates, does the request meet these?
1178 */
1179static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
1180 struct snd_usb_substream *subs)
1181{
1182 struct audioformat *fp;
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001183 int *rate_list;
Daniel Macke5779992010-03-04 19:46:13 +01001184 int count = 0, needs_knot = 0;
1185 int err;
1186
Clemens Ladisch5cd5d7c2012-05-18 18:00:43 +02001187 kfree(subs->rate_list.list);
1188 subs->rate_list.list = NULL;
1189
Daniel Macke5779992010-03-04 19:46:13 +01001190 list_for_each_entry(fp, &subs->fmt_list, list) {
1191 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
1192 return 0;
1193 count += fp->nr_rates;
1194 if (fp->rates & SNDRV_PCM_RATE_KNOT)
1195 needs_knot = 1;
1196 }
1197 if (!needs_knot)
1198 return 0;
1199
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001200 subs->rate_list.list = rate_list =
1201 kmalloc(sizeof(int) * count, GFP_KERNEL);
Jesper Juhl8a8d56b2010-10-29 20:40:23 +02001202 if (!subs->rate_list.list)
1203 return -ENOMEM;
1204 subs->rate_list.count = count;
Daniel Macke5779992010-03-04 19:46:13 +01001205 subs->rate_list.mask = 0;
1206 count = 0;
1207 list_for_each_entry(fp, &subs->fmt_list, list) {
1208 int i;
1209 for (i = 0; i < fp->nr_rates; i++)
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001210 rate_list[count++] = fp->rate_table[i];
Daniel Macke5779992010-03-04 19:46:13 +01001211 }
1212 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1213 &subs->rate_list);
1214 if (err < 0)
1215 return err;
1216
1217 return 0;
1218}
1219
1220
1221/*
1222 * set up the runtime hardware information.
1223 */
1224
1225static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1226{
Eldad Zack88766f02013-04-03 23:18:49 +02001227 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001228 unsigned int pt, ptmin;
1229 int param_period_time_if_needed;
1230 int err;
1231
1232 runtime->hw.formats = subs->formats;
1233
1234 runtime->hw.rate_min = 0x7fffffff;
1235 runtime->hw.rate_max = 0;
1236 runtime->hw.channels_min = 256;
1237 runtime->hw.channels_max = 0;
1238 runtime->hw.rates = 0;
1239 ptmin = UINT_MAX;
1240 /* check min/max rates and channels */
Eldad Zack88766f02013-04-03 23:18:49 +02001241 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001242 runtime->hw.rates |= fp->rates;
1243 if (runtime->hw.rate_min > fp->rate_min)
1244 runtime->hw.rate_min = fp->rate_min;
1245 if (runtime->hw.rate_max < fp->rate_max)
1246 runtime->hw.rate_max = fp->rate_max;
1247 if (runtime->hw.channels_min > fp->channels)
1248 runtime->hw.channels_min = fp->channels;
1249 if (runtime->hw.channels_max < fp->channels)
1250 runtime->hw.channels_max = fp->channels;
1251 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1252 /* FIXME: there might be more than one audio formats... */
1253 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1254 fp->frame_size;
1255 }
1256 pt = 125 * (1 << fp->datainterval);
1257 ptmin = min(ptmin, pt);
1258 }
Oliver Neukum88a85162011-03-11 14:51:12 +01001259 err = snd_usb_autoresume(subs->stream->chip);
1260 if (err < 0)
1261 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001262
1263 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Takashi Iwai978520b2012-10-12 15:12:55 +02001264 if (subs->speed == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +01001265 /* full speed devices have fixed data packet interval */
1266 ptmin = 1000;
1267 if (ptmin == 1000)
1268 /* if period time doesn't go below 1 ms, no rules needed */
1269 param_period_time_if_needed = -1;
1270 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1271 ptmin, UINT_MAX);
1272
1273 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1274 hw_rule_rate, subs,
1275 SNDRV_PCM_HW_PARAM_FORMAT,
1276 SNDRV_PCM_HW_PARAM_CHANNELS,
1277 param_period_time_if_needed,
1278 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001279 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001280 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1281 hw_rule_channels, subs,
1282 SNDRV_PCM_HW_PARAM_FORMAT,
1283 SNDRV_PCM_HW_PARAM_RATE,
1284 param_period_time_if_needed,
1285 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001286 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001287 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1288 hw_rule_format, subs,
1289 SNDRV_PCM_HW_PARAM_RATE,
1290 SNDRV_PCM_HW_PARAM_CHANNELS,
1291 param_period_time_if_needed,
1292 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001293 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001294 if (param_period_time_if_needed >= 0) {
1295 err = snd_pcm_hw_rule_add(runtime, 0,
1296 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1297 hw_rule_period_time, subs,
1298 SNDRV_PCM_HW_PARAM_FORMAT,
1299 SNDRV_PCM_HW_PARAM_CHANNELS,
1300 SNDRV_PCM_HW_PARAM_RATE,
1301 -1);
1302 if (err < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001303 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001304 }
1305 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001306 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001307 return 0;
Oliver Neukum88a85162011-03-11 14:51:12 +01001308
1309rep_err:
1310 snd_usb_autosuspend(subs->stream->chip);
1311 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001312}
1313
1314static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
1315{
1316 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1317 struct snd_pcm_runtime *runtime = substream->runtime;
1318 struct snd_usb_substream *subs = &as->substream[direction];
1319
1320 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +01001321 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +01001322 runtime->hw = snd_usb_hardware;
1323 runtime->private_data = subs;
1324 subs->pcm_substream = substream;
Oliver Neukum88a85162011-03-11 14:51:12 +01001325 /* runtime PM is also done there */
Daniel Mackd24f5062013-04-17 00:01:38 +08001326
1327 /* initialize DSD/DOP context */
1328 subs->dsd_dop.byte_idx = 0;
1329 subs->dsd_dop.channel = 0;
1330 subs->dsd_dop.marker = 1;
1331
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -03001332 return setup_hw_info(runtime, subs);
Daniel Macke5779992010-03-04 19:46:13 +01001333}
1334
1335static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1336{
1337 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1338 struct snd_usb_substream *subs = &as->substream[direction];
1339
Takashi Iwaib0db6062012-11-21 08:35:42 +01001340 stop_endpoints(subs, true);
Daniel Mack68e67f42012-07-12 13:08:40 +02001341
Takashi Iwai47ab1542015-08-25 16:09:00 +02001342 if (subs->interface >= 0 &&
1343 !snd_usb_lock_shutdown(subs->stream->chip)) {
Daniel Mack68e67f42012-07-12 13:08:40 +02001344 usb_set_interface(subs->dev, subs->interface, 0);
1345 subs->interface = -1;
Takashi Iwai47ab1542015-08-25 16:09:00 +02001346 snd_usb_unlock_shutdown(subs->stream->chip);
Daniel Mack68e67f42012-07-12 13:08:40 +02001347 }
1348
Daniel Macke5779992010-03-04 19:46:13 +01001349 subs->pcm_substream = NULL;
Oliver Neukum88a85162011-03-11 14:51:12 +01001350 snd_usb_autosuspend(subs->stream->chip);
Daniel Mackedcd3632012-04-12 13:51:12 +02001351
Daniel Mack68e67f42012-07-12 13:08:40 +02001352 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001353}
1354
1355/* Since a URB can handle only a single linear buffer, we must use double
1356 * buffering when the data to be transferred overflows the buffer boundary.
1357 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1358 * for all URBs.
1359 */
1360static void retire_capture_urb(struct snd_usb_substream *subs,
1361 struct urb *urb)
1362{
1363 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1364 unsigned int stride, frames, bytes, oldptr;
1365 int i, period_elapsed = 0;
1366 unsigned long flags;
1367 unsigned char *cp;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001368 int current_frame_number;
1369
1370 /* read frame number here, update pointer in critical section */
1371 current_frame_number = usb_get_current_frame_number(subs->dev);
Daniel Mackedcd3632012-04-12 13:51:12 +02001372
1373 stride = runtime->frame_bits >> 3;
1374
1375 for (i = 0; i < urb->number_of_packets; i++) {
Calvin Owens1539d4f2013-04-12 22:33:59 -05001376 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
Daniel Mackedcd3632012-04-12 13:51:12 +02001377 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001378 dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
1379 i, urb->iso_frame_desc[i].status);
Daniel Mackedcd3632012-04-12 13:51:12 +02001380 // continue;
1381 }
1382 bytes = urb->iso_frame_desc[i].actual_length;
1383 frames = bytes / stride;
1384 if (!subs->txfr_quirk)
1385 bytes = frames * stride;
1386 if (bytes % (runtime->sample_bits >> 3) != 0) {
Daniel Mackedcd3632012-04-12 13:51:12 +02001387 int oldbytes = bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001388 bytes = frames * stride;
Takashi Iwai03df65a2018-05-16 20:07:18 +02001389 dev_warn_ratelimited(&subs->dev->dev,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001390 "Corrected urb data len. %d->%d\n",
Daniel Mackedcd3632012-04-12 13:51:12 +02001391 oldbytes, bytes);
1392 }
1393 /* update the current pointer */
1394 spin_lock_irqsave(&subs->lock, flags);
1395 oldptr = subs->hwptr_done;
1396 subs->hwptr_done += bytes;
1397 if (subs->hwptr_done >= runtime->buffer_size * stride)
1398 subs->hwptr_done -= runtime->buffer_size * stride;
1399 frames = (bytes + (oldptr % stride)) / stride;
1400 subs->transfer_done += frames;
1401 if (subs->transfer_done >= runtime->period_size) {
1402 subs->transfer_done -= runtime->period_size;
1403 period_elapsed = 1;
1404 }
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001405 /* capture delay is by construction limited to one URB,
1406 * reset delays here
1407 */
1408 runtime->delay = subs->last_delay = 0;
1409
1410 /* realign last_frame_number */
1411 subs->last_frame_number = current_frame_number;
1412 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1413
Daniel Mackedcd3632012-04-12 13:51:12 +02001414 spin_unlock_irqrestore(&subs->lock, flags);
1415 /* copy a data chunk */
1416 if (oldptr + bytes > runtime->buffer_size * stride) {
1417 unsigned int bytes1 =
1418 runtime->buffer_size * stride - oldptr;
1419 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1420 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1421 } else {
1422 memcpy(runtime->dma_area + oldptr, cp, bytes);
1423 }
1424 }
1425
1426 if (period_elapsed)
1427 snd_pcm_period_elapsed(subs->pcm_substream);
1428}
1429
Daniel Mackd24f5062013-04-17 00:01:38 +08001430static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1431 struct urb *urb, unsigned int bytes)
1432{
1433 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1434 unsigned int stride = runtime->frame_bits >> 3;
1435 unsigned int dst_idx = 0;
1436 unsigned int src_idx = subs->hwptr_done;
1437 unsigned int wrap = runtime->buffer_size * stride;
1438 u8 *dst = urb->transfer_buffer;
1439 u8 *src = runtime->dma_area;
1440 u8 marker[] = { 0x05, 0xfa };
1441
1442 /*
1443 * The DSP DOP format defines a way to transport DSD samples over
1444 * normal PCM data endpoints. It requires stuffing of marker bytes
1445 * (0x05 and 0xfa, alternating per sample frame), and then expects
1446 * 2 additional bytes of actual payload. The whole frame is stored
1447 * LSB.
1448 *
1449 * Hence, for a stereo transport, the buffer layout looks like this,
1450 * where L refers to left channel samples and R to right.
1451 *
1452 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
1453 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
1454 * .....
1455 *
1456 */
1457
1458 while (bytes--) {
1459 if (++subs->dsd_dop.byte_idx == 3) {
1460 /* frame boundary? */
1461 dst[dst_idx++] = marker[subs->dsd_dop.marker];
1462 src_idx += 2;
1463 subs->dsd_dop.byte_idx = 0;
1464
1465 if (++subs->dsd_dop.channel % runtime->channels == 0) {
1466 /* alternate the marker */
1467 subs->dsd_dop.marker++;
1468 subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1469 subs->dsd_dop.channel = 0;
1470 }
1471 } else {
1472 /* stuff the DSD payload */
1473 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001474
1475 if (subs->cur_audiofmt->dsd_bitrev)
1476 dst[dst_idx++] = bitrev8(src[idx]);
1477 else
1478 dst[dst_idx++] = src[idx];
1479
Daniel Mackd24f5062013-04-17 00:01:38 +08001480 subs->hwptr_done++;
1481 }
1482 }
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001483 if (subs->hwptr_done >= runtime->buffer_size * stride)
1484 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001485}
1486
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001487static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
1488 int offset, int stride, unsigned int bytes)
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001489{
1490 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1491
1492 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1493 /* err, the transferred area goes over buffer boundary. */
1494 unsigned int bytes1 =
1495 runtime->buffer_size * stride - subs->hwptr_done;
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001496 memcpy(urb->transfer_buffer + offset,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001497 runtime->dma_area + subs->hwptr_done, bytes1);
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001498 memcpy(urb->transfer_buffer + offset + bytes1,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001499 runtime->dma_area, bytes - bytes1);
1500 } else {
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001501 memcpy(urb->transfer_buffer + offset,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001502 runtime->dma_area + subs->hwptr_done, bytes);
1503 }
1504 subs->hwptr_done += bytes;
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001505 if (subs->hwptr_done >= runtime->buffer_size * stride)
1506 subs->hwptr_done -= runtime->buffer_size * stride;
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001507}
1508
Ricard Wanderlofe0570442015-10-19 08:52:53 +02001509static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
1510 struct urb *urb, int stride,
1511 unsigned int bytes)
1512{
1513 __le32 packet_length;
1514 int i;
1515
1516 /* Put __le32 length descriptor at start of each packet. */
1517 for (i = 0; i < urb->number_of_packets; i++) {
1518 unsigned int length = urb->iso_frame_desc[i].length;
1519 unsigned int offset = urb->iso_frame_desc[i].offset;
1520
1521 packet_length = cpu_to_le32(length);
1522 offset += i * sizeof(packet_length);
1523 urb->iso_frame_desc[i].offset = offset;
1524 urb->iso_frame_desc[i].length += sizeof(packet_length);
1525 memcpy(urb->transfer_buffer + offset,
1526 &packet_length, sizeof(packet_length));
1527 copy_to_urb(subs, urb, offset + sizeof(packet_length),
1528 stride, length);
1529 }
1530 /* Adjust transfer size accordingly. */
1531 bytes += urb->number_of_packets * sizeof(packet_length);
1532 return bytes;
1533}
1534
Daniel Mackedcd3632012-04-12 13:51:12 +02001535static void prepare_playback_urb(struct snd_usb_substream *subs,
1536 struct urb *urb)
1537{
1538 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack245baf92012-08-30 18:52:30 +02001539 struct snd_usb_endpoint *ep = subs->data_endpoint;
Daniel Mackedcd3632012-04-12 13:51:12 +02001540 struct snd_urb_ctx *ctx = urb->context;
1541 unsigned int counts, frames, bytes;
1542 int i, stride, period_elapsed = 0;
1543 unsigned long flags;
1544
1545 stride = runtime->frame_bits >> 3;
1546
1547 frames = 0;
1548 urb->number_of_packets = 0;
1549 spin_lock_irqsave(&subs->lock, flags);
Alan Stern976b6c02013-09-24 15:51:58 -04001550 subs->frame_limit += ep->max_urb_frames;
Daniel Mackedcd3632012-04-12 13:51:12 +02001551 for (i = 0; i < ctx->packets; i++) {
Daniel Mack245baf92012-08-30 18:52:30 +02001552 if (ctx->packet_size[i])
1553 counts = ctx->packet_size[i];
1554 else
1555 counts = snd_usb_endpoint_next_packet_size(ep);
1556
Daniel Mackedcd3632012-04-12 13:51:12 +02001557 /* set up descriptor */
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001558 urb->iso_frame_desc[i].offset = frames * ep->stride;
1559 urb->iso_frame_desc[i].length = counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001560 frames += counts;
1561 urb->number_of_packets++;
1562 subs->transfer_done += counts;
1563 if (subs->transfer_done >= runtime->period_size) {
1564 subs->transfer_done -= runtime->period_size;
Alan Stern976b6c02013-09-24 15:51:58 -04001565 subs->frame_limit = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001566 period_elapsed = 1;
1567 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1568 if (subs->transfer_done > 0) {
1569 /* FIXME: fill-max mode is not
1570 * supported yet */
1571 frames -= subs->transfer_done;
1572 counts -= subs->transfer_done;
1573 urb->iso_frame_desc[i].length =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001574 counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001575 subs->transfer_done = 0;
1576 }
1577 i++;
1578 if (i < ctx->packets) {
1579 /* add a transfer delimiter */
1580 urb->iso_frame_desc[i].offset =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001581 frames * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001582 urb->iso_frame_desc[i].length = 0;
1583 urb->number_of_packets++;
1584 }
1585 break;
1586 }
1587 }
Alan Stern976b6c02013-09-24 15:51:58 -04001588 /* finish at the period boundary or after enough frames */
1589 if ((period_elapsed ||
1590 subs->transfer_done >= subs->frame_limit) &&
1591 !snd_usb_endpoint_implicit_feedback_sink(ep))
Daniel Mackedcd3632012-04-12 13:51:12 +02001592 break;
1593 }
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001594 bytes = frames * ep->stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001595
1596 if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1597 subs->cur_audiofmt->dsd_dop)) {
1598 fill_playback_urb_dsd_dop(subs, urb, bytes);
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001599 } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1600 subs->cur_audiofmt->dsd_bitrev)) {
1601 /* bit-reverse the bytes */
1602 u8 *buf = urb->transfer_buffer;
1603 for (i = 0; i < bytes; i++) {
1604 int idx = (subs->hwptr_done + i)
1605 % (runtime->buffer_size * stride);
1606 buf[i] = bitrev8(runtime->dma_area[idx]);
1607 }
1608
1609 subs->hwptr_done += bytes;
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001610 if (subs->hwptr_done >= runtime->buffer_size * stride)
1611 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001612 } else {
Daniel Mackd24f5062013-04-17 00:01:38 +08001613 /* usual PCM */
Ricard Wanderlofe0570442015-10-19 08:52:53 +02001614 if (!subs->tx_length_quirk)
1615 copy_to_urb(subs, urb, 0, stride, bytes);
1616 else
1617 bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
1618 /* bytes is now amount of outgoing data */
Daniel Mackedcd3632012-04-12 13:51:12 +02001619 }
Daniel Mackd24f5062013-04-17 00:01:38 +08001620
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001621 /* update delay with exact number of samples queued */
1622 runtime->delay = subs->last_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001623 runtime->delay += frames;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001624 subs->last_delay = runtime->delay;
1625
1626 /* realign last_frame_number */
1627 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1628 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1629
Pierre-Louis Bossartea33d352015-02-06 15:55:53 -06001630 if (subs->trigger_tstamp_pending_update) {
1631 /* this is the first actual URB submitted,
1632 * update trigger timestamp to reflect actual start time
1633 */
1634 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1635 subs->trigger_tstamp_pending_update = false;
1636 }
1637
Daniel Mackedcd3632012-04-12 13:51:12 +02001638 spin_unlock_irqrestore(&subs->lock, flags);
1639 urb->transfer_buffer_length = bytes;
1640 if (period_elapsed)
1641 snd_pcm_period_elapsed(subs->pcm_substream);
1642}
1643
1644/*
1645 * process after playback data complete
1646 * - decrease the delay count again
1647 */
1648static void retire_playback_urb(struct snd_usb_substream *subs,
1649 struct urb *urb)
1650{
1651 unsigned long flags;
1652 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001653 struct snd_usb_endpoint *ep = subs->data_endpoint;
1654 int processed = urb->transfer_buffer_length / ep->stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001655 int est_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001656
Takashi Iwai1213a202012-09-06 14:58:00 +02001657 /* ignore the delay accounting when procssed=0 is given, i.e.
1658 * silent payloads are procssed before handling the actual data
1659 */
1660 if (!processed)
1661 return;
1662
Daniel Mackedcd3632012-04-12 13:51:12 +02001663 spin_lock_irqsave(&subs->lock, flags);
Takashi Iwai48779a02012-11-23 16:00:37 +01001664 if (!subs->last_delay)
1665 goto out; /* short path */
1666
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001667 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1668 /* update delay with exact number of samples played */
1669 if (processed > subs->last_delay)
1670 subs->last_delay = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001671 else
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001672 subs->last_delay -= processed;
1673 runtime->delay = subs->last_delay;
1674
1675 /*
1676 * Report when delay estimate is off by more than 2ms.
1677 * The error should be lower than 2ms since the estimate relies
1678 * on two reads of a counter updated every ms.
1679 */
Sander Eikelenboomb7a77232014-05-02 15:09:27 +02001680 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1681 dev_dbg_ratelimited(&subs->dev->dev,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001682 "delay: estimated %d, actual %d\n",
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001683 est_delay, subs->last_delay);
1684
Takashi Iwai48779a02012-11-23 16:00:37 +01001685 if (!subs->running) {
1686 /* update last_frame_number for delay counting here since
1687 * prepare_playback_urb won't be called during pause
1688 */
1689 subs->last_frame_number =
1690 usb_get_current_frame_number(subs->dev) & 0xff;
1691 }
1692
1693 out:
Daniel Mackedcd3632012-04-12 13:51:12 +02001694 spin_unlock_irqrestore(&subs->lock, flags);
Daniel Macke5779992010-03-04 19:46:13 +01001695}
1696
1697static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1698{
1699 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1700}
1701
1702static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1703{
1704 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1705}
1706
1707static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1708{
1709 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1710}
1711
1712static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1713{
1714 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1715}
1716
Daniel Mackedcd3632012-04-12 13:51:12 +02001717static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1718 int cmd)
1719{
1720 struct snd_usb_substream *subs = substream->runtime->private_data;
1721
1722 switch (cmd) {
1723 case SNDRV_PCM_TRIGGER_START:
Pierre-Louis Bossartea33d352015-02-06 15:55:53 -06001724 subs->trigger_tstamp_pending_update = true;
Daniel Mackedcd3632012-04-12 13:51:12 +02001725 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1726 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1727 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001728 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001729 return 0;
1730 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001731 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001732 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001733 return 0;
1734 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1735 subs->data_endpoint->prepare_data_urb = NULL;
Takashi Iwai48779a02012-11-23 16:00:37 +01001736 /* keep retire_data_urb for delay calculation */
1737 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001738 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001739 return 0;
1740 }
1741
1742 return -EINVAL;
1743}
1744
Daniel Mackafe25962012-06-16 16:58:04 +02001745static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1746 int cmd)
Daniel Mackedcd3632012-04-12 13:51:12 +02001747{
1748 int err;
1749 struct snd_usb_substream *subs = substream->runtime->private_data;
1750
1751 switch (cmd) {
1752 case SNDRV_PCM_TRIGGER_START:
Ioan-Adrian Ratiube4e3ae2017-01-05 00:37:46 +02001753 err = start_endpoints(subs);
Daniel Mackedcd3632012-04-12 13:51:12 +02001754 if (err < 0)
1755 return err;
1756
1757 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001758 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001759 return 0;
1760 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001761 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001762 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001763 return 0;
1764 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1765 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001766 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001767 return 0;
1768 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1769 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001770 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001771 return 0;
1772 }
1773
1774 return -EINVAL;
1775}
1776
Daniel Macke5779992010-03-04 19:46:13 +01001777static struct snd_pcm_ops snd_usb_playback_ops = {
1778 .open = snd_usb_playback_open,
1779 .close = snd_usb_playback_close,
1780 .ioctl = snd_pcm_lib_ioctl,
1781 .hw_params = snd_usb_hw_params,
1782 .hw_free = snd_usb_hw_free,
1783 .prepare = snd_usb_pcm_prepare,
1784 .trigger = snd_usb_substream_playback_trigger,
1785 .pointer = snd_usb_pcm_pointer,
1786 .page = snd_pcm_lib_get_vmalloc_page,
1787 .mmap = snd_pcm_lib_mmap_vmalloc,
1788};
1789
1790static struct snd_pcm_ops snd_usb_capture_ops = {
1791 .open = snd_usb_capture_open,
1792 .close = snd_usb_capture_close,
1793 .ioctl = snd_pcm_lib_ioctl,
1794 .hw_params = snd_usb_hw_params,
1795 .hw_free = snd_usb_hw_free,
1796 .prepare = snd_usb_pcm_prepare,
1797 .trigger = snd_usb_substream_capture_trigger,
1798 .pointer = snd_usb_pcm_pointer,
1799 .page = snd_pcm_lib_get_vmalloc_page,
1800 .mmap = snd_pcm_lib_mmap_vmalloc,
1801};
1802
1803void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1804{
1805 snd_pcm_set_ops(pcm, stream,
1806 stream == SNDRV_PCM_STREAM_PLAYBACK ?
1807 &snd_usb_playback_ops : &snd_usb_capture_ops);
1808}