blob: adee1c0e2bdfa7a30efcebd6da58bcab8d619136 [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 &&
Ard van Breemenfaad3572019-08-02 13:52:14 +0200475 get_endpoint(alts, 0)->bSynchAddress != 0 &&
Eldad Zackf34d0652013-08-03 10:50:19 +0200476 ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
477 (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100478 dev_err(&dev->dev,
479 "%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
480 fmt->iface, fmt->altsetting,
Eldad Zackf34d0652013-08-03 10:50:19 +0200481 is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500482 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
483 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200484 return -EINVAL;
485 }
486
487 implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
488 == USB_ENDPOINT_USAGE_IMPLICIT_FB;
489
490 subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
491 alts, ep, !subs->direction,
492 implicit_fb ?
493 SND_USB_ENDPOINT_TYPE_DATA :
494 SND_USB_ENDPOINT_TYPE_SYNC);
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500495 if (!subs->sync_endpoint) {
496 if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
497 return 0;
Eldad Zackf34d0652013-08-03 10:50:19 +0200498 return -EINVAL;
Pierre-Louis Bossart395ae542015-08-14 17:19:43 -0500499 }
Eldad Zackf34d0652013-08-03 10:50:19 +0200500
501 subs->data_endpoint->sync_master = subs->sync_endpoint;
Daniel Macke5779992010-03-04 19:46:13 +0100502
Eldad Zack71bb64c2013-08-03 10:50:17 +0200503 return 0;
504}
505
506/*
507 * find a matching format and set up the interface
508 */
509static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
510{
511 struct usb_device *dev = subs->dev;
512 struct usb_host_interface *alts;
513 struct usb_interface_descriptor *altsd;
514 struct usb_interface *iface;
515 int err;
516
517 iface = usb_ifnum_to_if(dev, fmt->iface);
518 if (WARN_ON(!iface))
519 return -EINVAL;
520 alts = &iface->altsetting[fmt->altset_idx];
521 altsd = get_iface_desc(alts);
522 if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
523 return -EINVAL;
524
525 if (fmt == subs->cur_audiofmt)
526 return 0;
527
528 /* close the old interface */
529 if (subs->interface >= 0 && subs->interface != fmt->iface) {
Hemant Kumard4101882017-08-18 18:44:43 -0700530 err = usb_set_interface_timeout(subs->dev, subs->interface, 0,
531 MAX_SETALT_TIMEOUT_MS);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200532 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100533 dev_err(&dev->dev,
534 "%d:%d: return to setting 0 failed (%d)\n",
535 fmt->iface, fmt->altsetting, err);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200536 return -EIO;
537 }
538 subs->interface = -1;
539 subs->altset_idx = 0;
540 }
541
542 /* set interface */
543 if (subs->interface != fmt->iface ||
544 subs->altset_idx != fmt->altset_idx) {
Jurgen Kramer6874daa2014-11-28 17:32:54 +0100545
546 err = snd_usb_select_mode_quirk(subs, fmt);
547 if (err < 0)
548 return -EIO;
549
Hemant Kumard4101882017-08-18 18:44:43 -0700550 err = usb_set_interface_timeout(dev, fmt->iface,
551 fmt->altsetting, MAX_SETALT_TIMEOUT_MS);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200552 if (err < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100553 dev_err(&dev->dev,
554 "%d:%d: usb_set_interface failed (%d)\n",
555 fmt->iface, fmt->altsetting, err);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200556 return -EIO;
557 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100558 dev_dbg(&dev->dev, "setting usb interface %d:%d\n",
559 fmt->iface, fmt->altsetting);
Eldad Zack71bb64c2013-08-03 10:50:17 +0200560 subs->interface = fmt->iface;
561 subs->altset_idx = fmt->altset_idx;
562
563 snd_usb_set_interface_quirk(dev);
564 }
565
566 subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
567 alts, fmt->endpoint, subs->direction,
568 SND_USB_ENDPOINT_TYPE_DATA);
569
570 if (!subs->data_endpoint)
571 return -EINVAL;
572
573 err = set_sync_endpoint(subs, fmt, dev, alts, altsd);
574 if (err < 0)
575 return err;
576
Eldad Zackd133f2c2013-08-03 10:50:16 +0200577 err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
578 if (err < 0)
Daniel Macke5779992010-03-04 19:46:13 +0100579 return err;
580
581 subs->cur_audiofmt = fmt;
582
583 snd_usb_set_format_quirk(subs, fmt);
584
Daniel Macke5779992010-03-04 19:46:13 +0100585 return 0;
586}
587
Hemant Kumar9d9dac12016-03-07 14:46:31 -0800588int snd_usb_enable_audio_stream(struct snd_usb_substream *subs,
589 bool enable)
590{
591 struct audioformat *fmt;
592 struct usb_host_interface *alts;
593 struct usb_interface *iface;
594 int ret;
595
596 if (!enable) {
597 if (subs->interface >= 0) {
Hemant Kumard4101882017-08-18 18:44:43 -0700598 usb_set_interface_timeout(subs->dev, subs->interface, 0,
599 MAX_SETALT_TIMEOUT_MS);
Hemant Kumar9d9dac12016-03-07 14:46:31 -0800600 subs->altset_idx = 0;
601 subs->interface = -1;
602 subs->cur_audiofmt = NULL;
603 }
604
605 snd_usb_autosuspend(subs->stream->chip);
606 return 0;
607 }
608
609 snd_usb_autoresume(subs->stream->chip);
610 fmt = find_format(subs);
611 if (!fmt) {
Hemant Kumar43041092016-08-16 15:17:08 -0700612 dev_err(&subs->dev->dev,
Hemant Kumar9d9dac12016-03-07 14:46:31 -0800613 "cannot set format: format = %#x, rate = %d, channels = %d\n",
614 subs->pcm_format, subs->cur_rate, subs->channels);
615 return -EINVAL;
616 }
617
618 subs->altset_idx = 0;
619 subs->interface = -1;
620 if (atomic_read(&subs->stream->chip->shutdown)) {
621 ret = -ENODEV;
622 } else {
623 ret = set_format(subs, fmt);
624 if (ret < 0)
625 return ret;
626
627 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
628 if (!iface) {
629 dev_err(&subs->dev->dev, "Could not get iface %d\n",
630 subs->cur_audiofmt->iface);
631 return -ENODEV;
632 }
633
634 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
635 ret = snd_usb_init_sample_rate(subs->stream->chip,
636 subs->cur_audiofmt->iface,
637 alts,
638 subs->cur_audiofmt,
639 subs->cur_rate);
640 if (ret < 0) {
641 dev_err(&subs->dev->dev, "failed to set rate %d\n",
642 subs->cur_rate);
643 return ret;
644 }
645 }
646
647 subs->interface = fmt->iface;
648 subs->altset_idx = fmt->altset_idx;
649
650 return 0;
651}
652
Daniel Macke5779992010-03-04 19:46:13 +0100653/*
Eldad Zack0d9741c2012-12-03 20:30:09 +0100654 * Return the score of matching two audioformats.
655 * Veto the audioformat if:
656 * - It has no channels for some reason.
657 * - Requested PCM format is not supported.
658 * - Requested sample rate is not supported.
659 */
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100660static int match_endpoint_audioformats(struct snd_usb_substream *subs,
661 struct audioformat *fp,
662 struct audioformat *match, int rate,
663 snd_pcm_format_t pcm_format)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100664{
665 int i;
666 int score = 0;
667
668 if (fp->channels < 1) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100669 dev_dbg(&subs->dev->dev,
Vamsi Krishna Samavedam0836aae2016-11-03 17:21:02 -0700670 "%s: (fmt @%pK) no channels\n", __func__, fp);
Eldad Zack0d9741c2012-12-03 20:30:09 +0100671 return 0;
672 }
673
Eldad Zack74c34ca2013-04-23 01:00:41 +0200674 if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100675 dev_dbg(&subs->dev->dev,
Vamsi Krishna Samavedam0836aae2016-11-03 17:21:02 -0700676 "%s: (fmt @%pK) no match for format %d\n", __func__,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100677 fp, pcm_format);
678 return 0;
679 }
680
681 for (i = 0; i < fp->nr_rates; i++) {
682 if (fp->rate_table[i] == rate) {
683 score++;
684 break;
685 }
686 }
687 if (!score) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100688 dev_dbg(&subs->dev->dev,
Vamsi Krishna Samavedam0836aae2016-11-03 17:21:02 -0700689 "%s: (fmt @%pK) no match for rate %d\n", __func__,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100690 fp, rate);
691 return 0;
692 }
693
694 if (fp->channels == match->channels)
695 score++;
696
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100697 dev_dbg(&subs->dev->dev,
Vamsi Krishna Samavedam0836aae2016-11-03 17:21:02 -0700698 "%s: (fmt @%pK) score %d\n", __func__, fp, score);
Eldad Zack0d9741c2012-12-03 20:30:09 +0100699
700 return score;
701}
702
703/*
704 * Configure the sync ep using the rate and pcm format of the data ep.
705 */
706static int configure_sync_endpoint(struct snd_usb_substream *subs)
707{
708 int ret;
709 struct audioformat *fp;
710 struct audioformat *sync_fp = NULL;
711 int cur_score = 0;
712 int sync_period_bytes = subs->period_bytes;
713 struct snd_usb_substream *sync_subs =
714 &subs->stream->substream[subs->direction ^ 1];
715
Takashi Iwai31be5422013-01-10 14:06:38 +0100716 if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
717 !subs->stream)
718 return snd_usb_endpoint_set_params(subs->sync_endpoint,
719 subs->pcm_format,
720 subs->channels,
721 subs->period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400722 0, 0,
Takashi Iwai31be5422013-01-10 14:06:38 +0100723 subs->cur_rate,
724 subs->cur_audiofmt,
725 NULL);
726
Eldad Zack0d9741c2012-12-03 20:30:09 +0100727 /* Try to find the best matching audioformat. */
728 list_for_each_entry(fp, &sync_subs->fmt_list, list) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100729 int score = match_endpoint_audioformats(subs,
730 fp, subs->cur_audiofmt,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100731 subs->cur_rate, subs->pcm_format);
732
733 if (score > cur_score) {
734 sync_fp = fp;
735 cur_score = score;
736 }
737 }
738
739 if (unlikely(sync_fp == NULL)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100740 dev_err(&subs->dev->dev,
741 "%s: no valid audioformat for sync ep %x found\n",
Eldad Zack0d9741c2012-12-03 20:30:09 +0100742 __func__, sync_subs->ep_num);
743 return -EINVAL;
744 }
745
746 /*
747 * Recalculate the period bytes if channel number differ between
748 * data and sync ep audioformat.
749 */
750 if (sync_fp->channels != subs->channels) {
751 sync_period_bytes = (subs->period_bytes / subs->channels) *
752 sync_fp->channels;
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100753 dev_dbg(&subs->dev->dev,
754 "%s: adjusted sync ep period bytes (%d -> %d)\n",
Eldad Zack0d9741c2012-12-03 20:30:09 +0100755 __func__, subs->period_bytes, sync_period_bytes);
756 }
757
758 ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
759 subs->pcm_format,
760 sync_fp->channels,
761 sync_period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400762 0, 0,
Eldad Zack0d9741c2012-12-03 20:30:09 +0100763 subs->cur_rate,
764 sync_fp,
765 NULL);
766
767 return ret;
768}
769
770/*
Dylan Reid61a70952012-09-18 09:49:48 -0700771 * configure endpoint params
772 *
773 * called during initial setup and upon resume
774 */
775static int configure_endpoint(struct snd_usb_substream *subs)
776{
777 int ret;
778
Dylan Reid61a70952012-09-18 09:49:48 -0700779 /* format changed */
Takashi Iwaib0db6062012-11-21 08:35:42 +0100780 stop_endpoints(subs, true);
Dylan Reid61a70952012-09-18 09:49:48 -0700781 ret = snd_usb_endpoint_set_params(subs->data_endpoint,
782 subs->pcm_format,
783 subs->channels,
784 subs->period_bytes,
Alan Stern976b6c02013-09-24 15:51:58 -0400785 subs->period_frames,
786 subs->buffer_periods,
Dylan Reid61a70952012-09-18 09:49:48 -0700787 subs->cur_rate,
788 subs->cur_audiofmt,
789 subs->sync_endpoint);
790 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200791 return ret;
Dylan Reid61a70952012-09-18 09:49:48 -0700792
793 if (subs->sync_endpoint)
Eldad Zack0d9741c2012-12-03 20:30:09 +0100794 ret = configure_sync_endpoint(subs);
795
Dylan Reid61a70952012-09-18 09:49:48 -0700796 return ret;
797}
798
799/*
Daniel Macke5779992010-03-04 19:46:13 +0100800 * hw_params callback
801 *
802 * allocate a buffer and set the given audio format.
803 *
804 * so far we use a physically linear buffer although packetize transfer
805 * doesn't need a continuous area.
806 * if sg buffer is supported on the later version of alsa, we'll follow
807 * that.
808 */
809static int snd_usb_hw_params(struct snd_pcm_substream *substream,
810 struct snd_pcm_hw_params *hw_params)
811{
812 struct snd_usb_substream *subs = substream->runtime->private_data;
813 struct audioformat *fmt;
Dylan Reid61a70952012-09-18 09:49:48 -0700814 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100815
816 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
817 params_buffer_bytes(hw_params));
818 if (ret < 0)
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -0300819 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100820
Dylan Reid61a70952012-09-18 09:49:48 -0700821 subs->pcm_format = params_format(hw_params);
822 subs->period_bytes = params_period_bytes(hw_params);
Alan Stern976b6c02013-09-24 15:51:58 -0400823 subs->period_frames = params_period_size(hw_params);
824 subs->buffer_periods = params_periods(hw_params);
Dylan Reid61a70952012-09-18 09:49:48 -0700825 subs->channels = params_channels(hw_params);
826 subs->cur_rate = params_rate(hw_params);
827
828 fmt = find_format(subs);
Daniel Macke5779992010-03-04 19:46:13 +0100829 if (!fmt) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100830 dev_dbg(&subs->dev->dev,
831 "cannot set format: format = %#x, rate = %d, channels = %d\n",
Dylan Reid61a70952012-09-18 09:49:48 -0700832 subs->pcm_format, subs->cur_rate, subs->channels);
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -0300833 return -EINVAL;
Daniel Macke5779992010-03-04 19:46:13 +0100834 }
835
Takashi Iwai47ab1542015-08-25 16:09:00 +0200836 ret = snd_usb_lock_shutdown(subs->stream->chip);
837 if (ret < 0)
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -0300838 return ret;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200839 ret = set_format(subs, fmt);
840 snd_usb_unlock_shutdown(subs->stream->chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200841 if (ret < 0)
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -0300842 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100843
Dylan Reid61a70952012-09-18 09:49:48 -0700844 subs->interface = fmt->iface;
845 subs->altset_idx = fmt->altset_idx;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200846 subs->need_setup_ep = true;
Daniel Macke5779992010-03-04 19:46:13 +0100847
Dylan Reid61a70952012-09-18 09:49:48 -0700848 return 0;
Daniel Macke5779992010-03-04 19:46:13 +0100849}
850
851/*
852 * hw_free callback
853 *
854 * reset the audio format and release the buffer
855 */
856static int snd_usb_hw_free(struct snd_pcm_substream *substream)
857{
858 struct snd_usb_substream *subs = substream->runtime->private_data;
859
860 subs->cur_audiofmt = NULL;
861 subs->cur_rate = 0;
862 subs->period_bytes = 0;
Takashi Iwai47ab1542015-08-25 16:09:00 +0200863 if (!snd_usb_lock_shutdown(subs->stream->chip)) {
Takashi Iwaia9bb3622012-11-20 18:32:06 +0100864 stop_endpoints(subs, true);
Eldad Zack26de5d02013-10-06 22:31:07 +0200865 snd_usb_endpoint_deactivate(subs->sync_endpoint);
866 snd_usb_endpoint_deactivate(subs->data_endpoint);
Takashi Iwai47ab1542015-08-25 16:09:00 +0200867 snd_usb_unlock_shutdown(subs->stream->chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200868 }
Daniel Macke5779992010-03-04 19:46:13 +0100869 return snd_pcm_lib_free_vmalloc_buffer(substream);
870}
871
872/*
873 * prepare callback
874 *
875 * only a few subtle things...
876 */
877static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
878{
879 struct snd_pcm_runtime *runtime = substream->runtime;
880 struct snd_usb_substream *subs = runtime->private_data;
Dylan Reid61a70952012-09-18 09:49:48 -0700881 struct usb_host_interface *alts;
882 struct usb_interface *iface;
883 int ret;
Daniel Macke5779992010-03-04 19:46:13 +0100884
885 if (! subs->cur_audiofmt) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100886 dev_err(&subs->dev->dev, "no format is specified!\n");
Daniel Macke5779992010-03-04 19:46:13 +0100887 return -ENXIO;
888 }
889
Takashi Iwai47ab1542015-08-25 16:09:00 +0200890 ret = snd_usb_lock_shutdown(subs->stream->chip);
891 if (ret < 0)
892 return ret;
Takashi Iwai978520b2012-10-12 15:12:55 +0200893 if (snd_BUG_ON(!subs->data_endpoint)) {
894 ret = -EIO;
895 goto unlock;
896 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200897
Takashi Iwaif58161b2012-11-08 08:52:45 +0100898 snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
899 snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
900
Dylan Reid61a70952012-09-18 09:49:48 -0700901 ret = set_format(subs, subs->cur_audiofmt);
902 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200903 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700904
905 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
906 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
907 ret = snd_usb_init_sample_rate(subs->stream->chip,
908 subs->cur_audiofmt->iface,
909 alts,
910 subs->cur_audiofmt,
911 subs->cur_rate);
912 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200913 goto unlock;
Dylan Reid61a70952012-09-18 09:49:48 -0700914
Takashi Iwai384dc0852012-09-18 14:49:31 +0200915 if (subs->need_setup_ep) {
916 ret = configure_endpoint(subs);
917 if (ret < 0)
Takashi Iwai978520b2012-10-12 15:12:55 +0200918 goto unlock;
Takashi Iwai384dc0852012-09-18 14:49:31 +0200919 subs->need_setup_ep = false;
920 }
Dylan Reid61a70952012-09-18 09:49:48 -0700921
Daniel Macke5779992010-03-04 19:46:13 +0100922 /* some unit conversions in runtime */
Daniel Mackedcd3632012-04-12 13:51:12 +0200923 subs->data_endpoint->maxframesize =
924 bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
925 subs->data_endpoint->curframesize =
926 bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
Daniel Macke5779992010-03-04 19:46:13 +0100927
928 /* reset the pointer */
929 subs->hwptr_done = 0;
930 subs->transfer_done = 0;
Pierre-Louis Bossart294c4fb2011-09-06 19:15:34 -0500931 subs->last_delay = 0;
932 subs->last_frame_number = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100933 runtime->delay = 0;
934
Daniel Mackedcd3632012-04-12 13:51:12 +0200935 /* for playback, submit the URBs now; otherwise, the first hwptr_done
936 * updates for all URBs would happen at the same time when starting */
937 if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
Ioan-Adrian Ratiube4e3ae2017-01-05 00:37:46 +0200938 ret = start_endpoints(subs);
Daniel Mackedcd3632012-04-12 13:51:12 +0200939
Takashi Iwai978520b2012-10-12 15:12:55 +0200940 unlock:
Takashi Iwai47ab1542015-08-25 16:09:00 +0200941 snd_usb_unlock_shutdown(subs->stream->chip);
Takashi Iwai978520b2012-10-12 15:12:55 +0200942 return ret;
Daniel Macke5779992010-03-04 19:46:13 +0100943}
944
945static struct snd_pcm_hardware snd_usb_hardware =
946{
947 .info = SNDRV_PCM_INFO_MMAP |
948 SNDRV_PCM_INFO_MMAP_VALID |
949 SNDRV_PCM_INFO_BATCH |
950 SNDRV_PCM_INFO_INTERLEAVED |
951 SNDRV_PCM_INFO_BLOCK_TRANSFER |
952 SNDRV_PCM_INFO_PAUSE,
953 .buffer_bytes_max = 1024 * 1024,
954 .period_bytes_min = 64,
955 .period_bytes_max = 512 * 1024,
956 .periods_min = 2,
957 .periods_max = 1024,
958};
959
960static int hw_check_valid_format(struct snd_usb_substream *subs,
961 struct snd_pcm_hw_params *params,
962 struct audioformat *fp)
963{
964 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
965 struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
966 struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
967 struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100968 struct snd_mask check_fmts;
Daniel Macke5779992010-03-04 19:46:13 +0100969 unsigned int ptime;
970
971 /* check the format */
Clemens Ladisch015eb0b2010-03-04 19:46:15 +0100972 snd_mask_none(&check_fmts);
973 check_fmts.bits[0] = (u32)fp->formats;
974 check_fmts.bits[1] = (u32)(fp->formats >> 32);
975 snd_mask_intersect(&check_fmts, fmts);
976 if (snd_mask_empty(&check_fmts)) {
Daniel Macke5779992010-03-04 19:46:13 +0100977 hwc_debug(" > check: no supported format %d\n", fp->format);
978 return 0;
979 }
980 /* check the channels */
981 if (fp->channels < ct->min || fp->channels > ct->max) {
982 hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
983 return 0;
984 }
985 /* check the rate is within the range */
986 if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
987 hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max);
988 return 0;
989 }
990 if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
991 hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min);
992 return 0;
993 }
994 /* check whether the period time is >= the data packet interval */
Takashi Iwai978520b2012-10-12 15:12:55 +0200995 if (subs->speed != USB_SPEED_FULL) {
Daniel Macke5779992010-03-04 19:46:13 +0100996 ptime = 125 * (1 << fp->datainterval);
997 if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
998 hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max);
999 return 0;
1000 }
1001 }
1002 return 1;
1003}
1004
1005static int hw_rule_rate(struct snd_pcm_hw_params *params,
1006 struct snd_pcm_hw_rule *rule)
1007{
1008 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +02001009 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001010 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
1011 unsigned int rmin, rmax;
1012 int changed;
1013
1014 hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
1015 changed = 0;
1016 rmin = rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001017 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001018 if (!hw_check_valid_format(subs, params, fp))
1019 continue;
1020 if (changed++) {
1021 if (rmin > fp->rate_min)
1022 rmin = fp->rate_min;
1023 if (rmax < fp->rate_max)
1024 rmax = fp->rate_max;
1025 } else {
1026 rmin = fp->rate_min;
1027 rmax = fp->rate_max;
1028 }
1029 }
1030
1031 if (!changed) {
1032 hwc_debug(" --> get empty\n");
1033 it->empty = 1;
1034 return -EINVAL;
1035 }
1036
1037 changed = 0;
1038 if (it->min < rmin) {
1039 it->min = rmin;
1040 it->openmin = 0;
1041 changed = 1;
1042 }
1043 if (it->max > rmax) {
1044 it->max = rmax;
1045 it->openmax = 0;
1046 changed = 1;
1047 }
1048 if (snd_interval_checkempty(it)) {
1049 it->empty = 1;
1050 return -EINVAL;
1051 }
1052 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1053 return changed;
1054}
1055
1056
1057static int hw_rule_channels(struct snd_pcm_hw_params *params,
1058 struct snd_pcm_hw_rule *rule)
1059{
1060 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +02001061 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001062 struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
1063 unsigned int rmin, rmax;
1064 int changed;
1065
1066 hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
1067 changed = 0;
1068 rmin = rmax = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001069 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001070 if (!hw_check_valid_format(subs, params, fp))
1071 continue;
1072 if (changed++) {
1073 if (rmin > fp->channels)
1074 rmin = fp->channels;
1075 if (rmax < fp->channels)
1076 rmax = fp->channels;
1077 } else {
1078 rmin = fp->channels;
1079 rmax = fp->channels;
1080 }
1081 }
1082
1083 if (!changed) {
1084 hwc_debug(" --> get empty\n");
1085 it->empty = 1;
1086 return -EINVAL;
1087 }
1088
1089 changed = 0;
1090 if (it->min < rmin) {
1091 it->min = rmin;
1092 it->openmin = 0;
1093 changed = 1;
1094 }
1095 if (it->max > rmax) {
1096 it->max = rmax;
1097 it->openmax = 0;
1098 changed = 1;
1099 }
1100 if (snd_interval_checkempty(it)) {
1101 it->empty = 1;
1102 return -EINVAL;
1103 }
1104 hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
1105 return changed;
1106}
1107
1108static int hw_rule_format(struct snd_pcm_hw_params *params,
1109 struct snd_pcm_hw_rule *rule)
1110{
1111 struct snd_usb_substream *subs = rule->private;
Eldad Zack88766f02013-04-03 23:18:49 +02001112 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001113 struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1114 u64 fbits;
1115 u32 oldbits[2];
1116 int changed;
1117
1118 hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
1119 fbits = 0;
Eldad Zack88766f02013-04-03 23:18:49 +02001120 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001121 if (!hw_check_valid_format(subs, params, fp))
1122 continue;
Clemens Ladisch015eb0b2010-03-04 19:46:15 +01001123 fbits |= fp->formats;
Daniel Macke5779992010-03-04 19:46:13 +01001124 }
1125
1126 oldbits[0] = fmt->bits[0];
1127 oldbits[1] = fmt->bits[1];
1128 fmt->bits[0] &= (u32)fbits;
1129 fmt->bits[1] &= (u32)(fbits >> 32);
1130 if (!fmt->bits[0] && !fmt->bits[1]) {
1131 hwc_debug(" --> get empty\n");
1132 return -EINVAL;
1133 }
1134 changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1135 hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1136 return changed;
1137}
1138
1139static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1140 struct snd_pcm_hw_rule *rule)
1141{
1142 struct snd_usb_substream *subs = rule->private;
1143 struct audioformat *fp;
1144 struct snd_interval *it;
1145 unsigned char min_datainterval;
1146 unsigned int pmin;
1147 int changed;
1148
1149 it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1150 hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1151 min_datainterval = 0xff;
1152 list_for_each_entry(fp, &subs->fmt_list, list) {
1153 if (!hw_check_valid_format(subs, params, fp))
1154 continue;
1155 min_datainterval = min(min_datainterval, fp->datainterval);
1156 }
1157 if (min_datainterval == 0xff) {
Uwe Kleine-Königa7ce2e02010-07-12 17:15:44 +02001158 hwc_debug(" --> get empty\n");
Daniel Macke5779992010-03-04 19:46:13 +01001159 it->empty = 1;
1160 return -EINVAL;
1161 }
1162 pmin = 125 * (1 << min_datainterval);
1163 changed = 0;
1164 if (it->min < pmin) {
1165 it->min = pmin;
1166 it->openmin = 0;
1167 changed = 1;
1168 }
1169 if (snd_interval_checkempty(it)) {
1170 it->empty = 1;
1171 return -EINVAL;
1172 }
1173 hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
1174 return changed;
1175}
1176
1177/*
1178 * If the device supports unusual bit rates, does the request meet these?
1179 */
1180static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
1181 struct snd_usb_substream *subs)
1182{
1183 struct audioformat *fp;
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001184 int *rate_list;
Daniel Macke5779992010-03-04 19:46:13 +01001185 int count = 0, needs_knot = 0;
1186 int err;
1187
Clemens Ladisch5cd5d7c2012-05-18 18:00:43 +02001188 kfree(subs->rate_list.list);
1189 subs->rate_list.list = NULL;
1190
Daniel Macke5779992010-03-04 19:46:13 +01001191 list_for_each_entry(fp, &subs->fmt_list, list) {
1192 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
1193 return 0;
1194 count += fp->nr_rates;
1195 if (fp->rates & SNDRV_PCM_RATE_KNOT)
1196 needs_knot = 1;
1197 }
1198 if (!needs_knot)
1199 return 0;
1200
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001201 subs->rate_list.list = rate_list =
1202 kmalloc(sizeof(int) * count, GFP_KERNEL);
Jesper Juhl8a8d56b2010-10-29 20:40:23 +02001203 if (!subs->rate_list.list)
1204 return -ENOMEM;
1205 subs->rate_list.count = count;
Daniel Macke5779992010-03-04 19:46:13 +01001206 subs->rate_list.mask = 0;
1207 count = 0;
1208 list_for_each_entry(fp, &subs->fmt_list, list) {
1209 int i;
1210 for (i = 0; i < fp->nr_rates; i++)
Takashi Iwai0717d0f2012-03-15 16:14:38 +01001211 rate_list[count++] = fp->rate_table[i];
Daniel Macke5779992010-03-04 19:46:13 +01001212 }
1213 err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1214 &subs->rate_list);
1215 if (err < 0)
1216 return err;
1217
1218 return 0;
1219}
1220
1221
1222/*
1223 * set up the runtime hardware information.
1224 */
1225
1226static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1227{
Eldad Zack88766f02013-04-03 23:18:49 +02001228 struct audioformat *fp;
Daniel Macke5779992010-03-04 19:46:13 +01001229 unsigned int pt, ptmin;
1230 int param_period_time_if_needed;
1231 int err;
1232
1233 runtime->hw.formats = subs->formats;
1234
1235 runtime->hw.rate_min = 0x7fffffff;
1236 runtime->hw.rate_max = 0;
1237 runtime->hw.channels_min = 256;
1238 runtime->hw.channels_max = 0;
1239 runtime->hw.rates = 0;
1240 ptmin = UINT_MAX;
1241 /* check min/max rates and channels */
Eldad Zack88766f02013-04-03 23:18:49 +02001242 list_for_each_entry(fp, &subs->fmt_list, list) {
Daniel Macke5779992010-03-04 19:46:13 +01001243 runtime->hw.rates |= fp->rates;
1244 if (runtime->hw.rate_min > fp->rate_min)
1245 runtime->hw.rate_min = fp->rate_min;
1246 if (runtime->hw.rate_max < fp->rate_max)
1247 runtime->hw.rate_max = fp->rate_max;
1248 if (runtime->hw.channels_min > fp->channels)
1249 runtime->hw.channels_min = fp->channels;
1250 if (runtime->hw.channels_max < fp->channels)
1251 runtime->hw.channels_max = fp->channels;
1252 if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1253 /* FIXME: there might be more than one audio formats... */
1254 runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1255 fp->frame_size;
1256 }
1257 pt = 125 * (1 << fp->datainterval);
1258 ptmin = min(ptmin, pt);
1259 }
Oliver Neukum88a85162011-03-11 14:51:12 +01001260 err = snd_usb_autoresume(subs->stream->chip);
1261 if (err < 0)
1262 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001263
1264 param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
Takashi Iwai978520b2012-10-12 15:12:55 +02001265 if (subs->speed == USB_SPEED_FULL)
Daniel Macke5779992010-03-04 19:46:13 +01001266 /* full speed devices have fixed data packet interval */
1267 ptmin = 1000;
1268 if (ptmin == 1000)
1269 /* if period time doesn't go below 1 ms, no rules needed */
1270 param_period_time_if_needed = -1;
1271 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1272 ptmin, UINT_MAX);
1273
1274 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1275 hw_rule_rate, subs,
1276 SNDRV_PCM_HW_PARAM_FORMAT,
1277 SNDRV_PCM_HW_PARAM_CHANNELS,
1278 param_period_time_if_needed,
1279 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001280 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001281 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1282 hw_rule_channels, subs,
1283 SNDRV_PCM_HW_PARAM_FORMAT,
1284 SNDRV_PCM_HW_PARAM_RATE,
1285 param_period_time_if_needed,
1286 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001287 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001288 if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1289 hw_rule_format, subs,
1290 SNDRV_PCM_HW_PARAM_RATE,
1291 SNDRV_PCM_HW_PARAM_CHANNELS,
1292 param_period_time_if_needed,
1293 -1)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001294 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001295 if (param_period_time_if_needed >= 0) {
1296 err = snd_pcm_hw_rule_add(runtime, 0,
1297 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1298 hw_rule_period_time, subs,
1299 SNDRV_PCM_HW_PARAM_FORMAT,
1300 SNDRV_PCM_HW_PARAM_CHANNELS,
1301 SNDRV_PCM_HW_PARAM_RATE,
1302 -1);
1303 if (err < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001304 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001305 }
1306 if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
Oliver Neukum88a85162011-03-11 14:51:12 +01001307 goto rep_err;
Daniel Macke5779992010-03-04 19:46:13 +01001308 return 0;
Oliver Neukum88a85162011-03-11 14:51:12 +01001309
1310rep_err:
1311 snd_usb_autosuspend(subs->stream->chip);
1312 return err;
Daniel Macke5779992010-03-04 19:46:13 +01001313}
1314
1315static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
1316{
1317 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1318 struct snd_pcm_runtime *runtime = substream->runtime;
1319 struct snd_usb_substream *subs = &as->substream[direction];
1320
1321 subs->interface = -1;
Clemens Ladische11b4e02010-03-04 19:46:14 +01001322 subs->altset_idx = 0;
Daniel Macke5779992010-03-04 19:46:13 +01001323 runtime->hw = snd_usb_hardware;
1324 runtime->private_data = subs;
1325 subs->pcm_substream = substream;
Oliver Neukum88a85162011-03-11 14:51:12 +01001326 /* runtime PM is also done there */
Daniel Mackd24f5062013-04-17 00:01:38 +08001327
1328 /* initialize DSD/DOP context */
1329 subs->dsd_dop.byte_idx = 0;
1330 subs->dsd_dop.channel = 0;
1331 subs->dsd_dop.marker = 1;
1332
Mauro Carvalho Chehabc89178f2016-03-31 09:57:29 -03001333 return setup_hw_info(runtime, subs);
Daniel Macke5779992010-03-04 19:46:13 +01001334}
1335
1336static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1337{
1338 struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1339 struct snd_usb_substream *subs = &as->substream[direction];
1340
Takashi Iwaib0db6062012-11-21 08:35:42 +01001341 stop_endpoints(subs, true);
Daniel Mack68e67f42012-07-12 13:08:40 +02001342
Takashi Iwai47ab1542015-08-25 16:09:00 +02001343 if (subs->interface >= 0 &&
1344 !snd_usb_lock_shutdown(subs->stream->chip)) {
Daniel Mack68e67f42012-07-12 13:08:40 +02001345 usb_set_interface(subs->dev, subs->interface, 0);
1346 subs->interface = -1;
Takashi Iwai47ab1542015-08-25 16:09:00 +02001347 snd_usb_unlock_shutdown(subs->stream->chip);
Daniel Mack68e67f42012-07-12 13:08:40 +02001348 }
1349
Daniel Macke5779992010-03-04 19:46:13 +01001350 subs->pcm_substream = NULL;
Oliver Neukum88a85162011-03-11 14:51:12 +01001351 snd_usb_autosuspend(subs->stream->chip);
Daniel Mackedcd3632012-04-12 13:51:12 +02001352
Daniel Mack68e67f42012-07-12 13:08:40 +02001353 return 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001354}
1355
1356/* Since a URB can handle only a single linear buffer, we must use double
1357 * buffering when the data to be transferred overflows the buffer boundary.
1358 * To avoid inconsistencies when updating hwptr_done, we use double buffering
1359 * for all URBs.
1360 */
1361static void retire_capture_urb(struct snd_usb_substream *subs,
1362 struct urb *urb)
1363{
1364 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1365 unsigned int stride, frames, bytes, oldptr;
1366 int i, period_elapsed = 0;
1367 unsigned long flags;
1368 unsigned char *cp;
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001369 int current_frame_number;
1370
1371 /* read frame number here, update pointer in critical section */
1372 current_frame_number = usb_get_current_frame_number(subs->dev);
Daniel Mackedcd3632012-04-12 13:51:12 +02001373
1374 stride = runtime->frame_bits >> 3;
1375
1376 for (i = 0; i < urb->number_of_packets; i++) {
Calvin Owens1539d4f2013-04-12 22:33:59 -05001377 cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
Daniel Mackedcd3632012-04-12 13:51:12 +02001378 if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001379 dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
1380 i, urb->iso_frame_desc[i].status);
Daniel Mackedcd3632012-04-12 13:51:12 +02001381 // continue;
1382 }
1383 bytes = urb->iso_frame_desc[i].actual_length;
1384 frames = bytes / stride;
1385 if (!subs->txfr_quirk)
1386 bytes = frames * stride;
1387 if (bytes % (runtime->sample_bits >> 3) != 0) {
Daniel Mackedcd3632012-04-12 13:51:12 +02001388 int oldbytes = bytes;
Daniel Mackedcd3632012-04-12 13:51:12 +02001389 bytes = frames * stride;
Takashi Iwai03df65a2018-05-16 20:07:18 +02001390 dev_warn_ratelimited(&subs->dev->dev,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001391 "Corrected urb data len. %d->%d\n",
Daniel Mackedcd3632012-04-12 13:51:12 +02001392 oldbytes, bytes);
1393 }
1394 /* update the current pointer */
1395 spin_lock_irqsave(&subs->lock, flags);
1396 oldptr = subs->hwptr_done;
1397 subs->hwptr_done += bytes;
1398 if (subs->hwptr_done >= runtime->buffer_size * stride)
1399 subs->hwptr_done -= runtime->buffer_size * stride;
1400 frames = (bytes + (oldptr % stride)) / stride;
1401 subs->transfer_done += frames;
1402 if (subs->transfer_done >= runtime->period_size) {
1403 subs->transfer_done -= runtime->period_size;
1404 period_elapsed = 1;
1405 }
Pierre-Louis Bossarte4cc6152012-12-19 11:39:05 -06001406 /* capture delay is by construction limited to one URB,
1407 * reset delays here
1408 */
1409 runtime->delay = subs->last_delay = 0;
1410
1411 /* realign last_frame_number */
1412 subs->last_frame_number = current_frame_number;
1413 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1414
Daniel Mackedcd3632012-04-12 13:51:12 +02001415 spin_unlock_irqrestore(&subs->lock, flags);
1416 /* copy a data chunk */
1417 if (oldptr + bytes > runtime->buffer_size * stride) {
1418 unsigned int bytes1 =
1419 runtime->buffer_size * stride - oldptr;
1420 memcpy(runtime->dma_area + oldptr, cp, bytes1);
1421 memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1422 } else {
1423 memcpy(runtime->dma_area + oldptr, cp, bytes);
1424 }
1425 }
1426
1427 if (period_elapsed)
1428 snd_pcm_period_elapsed(subs->pcm_substream);
1429}
1430
Daniel Mackd24f5062013-04-17 00:01:38 +08001431static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1432 struct urb *urb, unsigned int bytes)
1433{
1434 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1435 unsigned int stride = runtime->frame_bits >> 3;
1436 unsigned int dst_idx = 0;
1437 unsigned int src_idx = subs->hwptr_done;
1438 unsigned int wrap = runtime->buffer_size * stride;
1439 u8 *dst = urb->transfer_buffer;
1440 u8 *src = runtime->dma_area;
1441 u8 marker[] = { 0x05, 0xfa };
1442
1443 /*
1444 * The DSP DOP format defines a way to transport DSD samples over
1445 * normal PCM data endpoints. It requires stuffing of marker bytes
1446 * (0x05 and 0xfa, alternating per sample frame), and then expects
1447 * 2 additional bytes of actual payload. The whole frame is stored
1448 * LSB.
1449 *
1450 * Hence, for a stereo transport, the buffer layout looks like this,
1451 * where L refers to left channel samples and R to right.
1452 *
1453 * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa
1454 * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa
1455 * .....
1456 *
1457 */
1458
1459 while (bytes--) {
1460 if (++subs->dsd_dop.byte_idx == 3) {
1461 /* frame boundary? */
1462 dst[dst_idx++] = marker[subs->dsd_dop.marker];
1463 src_idx += 2;
1464 subs->dsd_dop.byte_idx = 0;
1465
1466 if (++subs->dsd_dop.channel % runtime->channels == 0) {
1467 /* alternate the marker */
1468 subs->dsd_dop.marker++;
1469 subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1470 subs->dsd_dop.channel = 0;
1471 }
1472 } else {
1473 /* stuff the DSD payload */
1474 int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001475
1476 if (subs->cur_audiofmt->dsd_bitrev)
1477 dst[dst_idx++] = bitrev8(src[idx]);
1478 else
1479 dst[dst_idx++] = src[idx];
1480
Daniel Mackd24f5062013-04-17 00:01:38 +08001481 subs->hwptr_done++;
1482 }
1483 }
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001484 if (subs->hwptr_done >= runtime->buffer_size * stride)
1485 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001486}
1487
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001488static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
1489 int offset, int stride, unsigned int bytes)
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001490{
1491 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1492
1493 if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1494 /* err, the transferred area goes over buffer boundary. */
1495 unsigned int bytes1 =
1496 runtime->buffer_size * stride - subs->hwptr_done;
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001497 memcpy(urb->transfer_buffer + offset,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001498 runtime->dma_area + subs->hwptr_done, bytes1);
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001499 memcpy(urb->transfer_buffer + offset + bytes1,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001500 runtime->dma_area, bytes - bytes1);
1501 } else {
Ricard Wanderlofb97a9362015-10-19 08:52:52 +02001502 memcpy(urb->transfer_buffer + offset,
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001503 runtime->dma_area + subs->hwptr_done, bytes);
1504 }
1505 subs->hwptr_done += bytes;
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001506 if (subs->hwptr_done >= runtime->buffer_size * stride)
1507 subs->hwptr_done -= runtime->buffer_size * stride;
Ricard Wanderlof07a40c2f2015-10-19 08:52:49 +02001508}
1509
Ricard Wanderlofe0570442015-10-19 08:52:53 +02001510static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
1511 struct urb *urb, int stride,
1512 unsigned int bytes)
1513{
1514 __le32 packet_length;
1515 int i;
1516
1517 /* Put __le32 length descriptor at start of each packet. */
1518 for (i = 0; i < urb->number_of_packets; i++) {
1519 unsigned int length = urb->iso_frame_desc[i].length;
1520 unsigned int offset = urb->iso_frame_desc[i].offset;
1521
1522 packet_length = cpu_to_le32(length);
1523 offset += i * sizeof(packet_length);
1524 urb->iso_frame_desc[i].offset = offset;
1525 urb->iso_frame_desc[i].length += sizeof(packet_length);
1526 memcpy(urb->transfer_buffer + offset,
1527 &packet_length, sizeof(packet_length));
1528 copy_to_urb(subs, urb, offset + sizeof(packet_length),
1529 stride, length);
1530 }
1531 /* Adjust transfer size accordingly. */
1532 bytes += urb->number_of_packets * sizeof(packet_length);
1533 return bytes;
1534}
1535
Daniel Mackedcd3632012-04-12 13:51:12 +02001536static void prepare_playback_urb(struct snd_usb_substream *subs,
1537 struct urb *urb)
1538{
1539 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack245baf92012-08-30 18:52:30 +02001540 struct snd_usb_endpoint *ep = subs->data_endpoint;
Daniel Mackedcd3632012-04-12 13:51:12 +02001541 struct snd_urb_ctx *ctx = urb->context;
1542 unsigned int counts, frames, bytes;
1543 int i, stride, period_elapsed = 0;
1544 unsigned long flags;
1545
1546 stride = runtime->frame_bits >> 3;
1547
1548 frames = 0;
1549 urb->number_of_packets = 0;
1550 spin_lock_irqsave(&subs->lock, flags);
Alan Stern976b6c02013-09-24 15:51:58 -04001551 subs->frame_limit += ep->max_urb_frames;
Daniel Mackedcd3632012-04-12 13:51:12 +02001552 for (i = 0; i < ctx->packets; i++) {
Daniel Mack245baf92012-08-30 18:52:30 +02001553 if (ctx->packet_size[i])
1554 counts = ctx->packet_size[i];
1555 else
1556 counts = snd_usb_endpoint_next_packet_size(ep);
1557
Daniel Mackedcd3632012-04-12 13:51:12 +02001558 /* set up descriptor */
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001559 urb->iso_frame_desc[i].offset = frames * ep->stride;
1560 urb->iso_frame_desc[i].length = counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001561 frames += counts;
1562 urb->number_of_packets++;
1563 subs->transfer_done += counts;
1564 if (subs->transfer_done >= runtime->period_size) {
1565 subs->transfer_done -= runtime->period_size;
Alan Stern976b6c02013-09-24 15:51:58 -04001566 subs->frame_limit = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001567 period_elapsed = 1;
1568 if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1569 if (subs->transfer_done > 0) {
1570 /* FIXME: fill-max mode is not
1571 * supported yet */
1572 frames -= subs->transfer_done;
1573 counts -= subs->transfer_done;
1574 urb->iso_frame_desc[i].length =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001575 counts * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001576 subs->transfer_done = 0;
1577 }
1578 i++;
1579 if (i < ctx->packets) {
1580 /* add a transfer delimiter */
1581 urb->iso_frame_desc[i].offset =
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001582 frames * ep->stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001583 urb->iso_frame_desc[i].length = 0;
1584 urb->number_of_packets++;
1585 }
1586 break;
1587 }
1588 }
Alan Stern976b6c02013-09-24 15:51:58 -04001589 /* finish at the period boundary or after enough frames */
1590 if ((period_elapsed ||
1591 subs->transfer_done >= subs->frame_limit) &&
1592 !snd_usb_endpoint_implicit_feedback_sink(ep))
Daniel Mackedcd3632012-04-12 13:51:12 +02001593 break;
1594 }
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001595 bytes = frames * ep->stride;
Daniel Mackd24f5062013-04-17 00:01:38 +08001596
1597 if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1598 subs->cur_audiofmt->dsd_dop)) {
1599 fill_playback_urb_dsd_dop(subs, urb, bytes);
Daniel Mack44dcbbb2013-04-17 00:01:39 +08001600 } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1601 subs->cur_audiofmt->dsd_bitrev)) {
1602 /* bit-reverse the bytes */
1603 u8 *buf = urb->transfer_buffer;
1604 for (i = 0; i < bytes; i++) {
1605 int idx = (subs->hwptr_done + i)
1606 % (runtime->buffer_size * stride);
1607 buf[i] = bitrev8(runtime->dma_area[idx]);
1608 }
1609
1610 subs->hwptr_done += bytes;
Ricard Wanderlof4c4e4392015-10-19 08:52:50 +02001611 if (subs->hwptr_done >= runtime->buffer_size * stride)
1612 subs->hwptr_done -= runtime->buffer_size * stride;
Daniel Mackedcd3632012-04-12 13:51:12 +02001613 } else {
Daniel Mackd24f5062013-04-17 00:01:38 +08001614 /* usual PCM */
Ricard Wanderlofe0570442015-10-19 08:52:53 +02001615 if (!subs->tx_length_quirk)
1616 copy_to_urb(subs, urb, 0, stride, bytes);
1617 else
1618 bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
1619 /* bytes is now amount of outgoing data */
Daniel Mackedcd3632012-04-12 13:51:12 +02001620 }
Daniel Mackd24f5062013-04-17 00:01:38 +08001621
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001622 /* update delay with exact number of samples queued */
1623 runtime->delay = subs->last_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001624 runtime->delay += frames;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001625 subs->last_delay = runtime->delay;
1626
1627 /* realign last_frame_number */
1628 subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1629 subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1630
Pierre-Louis Bossartea33d352015-02-06 15:55:53 -06001631 if (subs->trigger_tstamp_pending_update) {
1632 /* this is the first actual URB submitted,
1633 * update trigger timestamp to reflect actual start time
1634 */
1635 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
1636 subs->trigger_tstamp_pending_update = false;
1637 }
1638
Daniel Mackedcd3632012-04-12 13:51:12 +02001639 spin_unlock_irqrestore(&subs->lock, flags);
1640 urb->transfer_buffer_length = bytes;
1641 if (period_elapsed)
1642 snd_pcm_period_elapsed(subs->pcm_substream);
1643}
1644
1645/*
1646 * process after playback data complete
1647 * - decrease the delay count again
1648 */
1649static void retire_playback_urb(struct snd_usb_substream *subs,
1650 struct urb *urb)
1651{
1652 unsigned long flags;
1653 struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
Daniel Mack8a2a74d2013-04-17 00:01:37 +08001654 struct snd_usb_endpoint *ep = subs->data_endpoint;
1655 int processed = urb->transfer_buffer_length / ep->stride;
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001656 int est_delay;
Daniel Mackedcd3632012-04-12 13:51:12 +02001657
Takashi Iwai1213a202012-09-06 14:58:00 +02001658 /* ignore the delay accounting when procssed=0 is given, i.e.
1659 * silent payloads are procssed before handling the actual data
1660 */
1661 if (!processed)
1662 return;
1663
Daniel Mackedcd3632012-04-12 13:51:12 +02001664 spin_lock_irqsave(&subs->lock, flags);
Takashi Iwai48779a02012-11-23 16:00:37 +01001665 if (!subs->last_delay)
1666 goto out; /* short path */
1667
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001668 est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1669 /* update delay with exact number of samples played */
1670 if (processed > subs->last_delay)
1671 subs->last_delay = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001672 else
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001673 subs->last_delay -= processed;
1674 runtime->delay = subs->last_delay;
1675
1676 /*
1677 * Report when delay estimate is off by more than 2ms.
1678 * The error should be lower than 2ms since the estimate relies
1679 * on two reads of a counter updated every ms.
1680 */
Sander Eikelenboomb7a77232014-05-02 15:09:27 +02001681 if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1682 dev_dbg_ratelimited(&subs->dev->dev,
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001683 "delay: estimated %d, actual %d\n",
Daniel Mackfbcfbf52012-08-30 18:52:29 +02001684 est_delay, subs->last_delay);
1685
Takashi Iwai48779a02012-11-23 16:00:37 +01001686 if (!subs->running) {
1687 /* update last_frame_number for delay counting here since
1688 * prepare_playback_urb won't be called during pause
1689 */
1690 subs->last_frame_number =
1691 usb_get_current_frame_number(subs->dev) & 0xff;
1692 }
1693
1694 out:
Daniel Mackedcd3632012-04-12 13:51:12 +02001695 spin_unlock_irqrestore(&subs->lock, flags);
Daniel Macke5779992010-03-04 19:46:13 +01001696}
1697
1698static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1699{
1700 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1701}
1702
1703static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1704{
1705 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1706}
1707
1708static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1709{
1710 return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1711}
1712
1713static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1714{
1715 return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1716}
1717
Daniel Mackedcd3632012-04-12 13:51:12 +02001718static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1719 int cmd)
1720{
1721 struct snd_usb_substream *subs = substream->runtime->private_data;
1722
1723 switch (cmd) {
1724 case SNDRV_PCM_TRIGGER_START:
Pierre-Louis Bossartea33d352015-02-06 15:55:53 -06001725 subs->trigger_tstamp_pending_update = true;
Daniel Mackedcd3632012-04-12 13:51:12 +02001726 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1727 subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1728 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001729 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001730 return 0;
1731 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001732 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001733 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001734 return 0;
1735 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1736 subs->data_endpoint->prepare_data_urb = NULL;
Takashi Iwai48779a02012-11-23 16:00:37 +01001737 /* keep retire_data_urb for delay calculation */
1738 subs->data_endpoint->retire_data_urb = retire_playback_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001739 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001740 return 0;
1741 }
1742
1743 return -EINVAL;
1744}
1745
Daniel Mackafe25962012-06-16 16:58:04 +02001746static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1747 int cmd)
Daniel Mackedcd3632012-04-12 13:51:12 +02001748{
1749 int err;
1750 struct snd_usb_substream *subs = substream->runtime->private_data;
1751
1752 switch (cmd) {
1753 case SNDRV_PCM_TRIGGER_START:
Ioan-Adrian Ratiube4e3ae2017-01-05 00:37:46 +02001754 err = start_endpoints(subs);
Daniel Mackedcd3632012-04-12 13:51:12 +02001755 if (err < 0)
1756 return err;
1757
1758 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001759 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001760 return 0;
1761 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwaia9bb3622012-11-20 18:32:06 +01001762 stop_endpoints(subs, false);
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001763 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001764 return 0;
1765 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1766 subs->data_endpoint->retire_data_urb = NULL;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001767 subs->running = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +02001768 return 0;
1769 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1770 subs->data_endpoint->retire_data_urb = retire_capture_urb;
Daniel Mack97f8d3b2012-05-21 12:47:36 +02001771 subs->running = 1;
Daniel Mackedcd3632012-04-12 13:51:12 +02001772 return 0;
1773 }
1774
1775 return -EINVAL;
1776}
1777
Daniel Macke5779992010-03-04 19:46:13 +01001778static struct snd_pcm_ops snd_usb_playback_ops = {
1779 .open = snd_usb_playback_open,
1780 .close = snd_usb_playback_close,
1781 .ioctl = snd_pcm_lib_ioctl,
1782 .hw_params = snd_usb_hw_params,
1783 .hw_free = snd_usb_hw_free,
1784 .prepare = snd_usb_pcm_prepare,
1785 .trigger = snd_usb_substream_playback_trigger,
1786 .pointer = snd_usb_pcm_pointer,
1787 .page = snd_pcm_lib_get_vmalloc_page,
1788 .mmap = snd_pcm_lib_mmap_vmalloc,
1789};
1790
1791static struct snd_pcm_ops snd_usb_capture_ops = {
1792 .open = snd_usb_capture_open,
1793 .close = snd_usb_capture_close,
1794 .ioctl = snd_pcm_lib_ioctl,
1795 .hw_params = snd_usb_hw_params,
1796 .hw_free = snd_usb_hw_free,
1797 .prepare = snd_usb_pcm_prepare,
1798 .trigger = snd_usb_substream_capture_trigger,
1799 .pointer = snd_usb_pcm_pointer,
1800 .page = snd_pcm_lib_get_vmalloc_page,
1801 .mmap = snd_pcm_lib_mmap_vmalloc,
1802};
1803
1804void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1805{
1806 snd_pcm_set_ops(pcm, stream,
1807 stream == SNDRV_PCM_STREAM_PLAYBACK ?
1808 &snd_usb_playback_ops : &snd_usb_capture_ops);
1809}