xref: /openbmc/linux/sound/usb/pcm.c (revision 5bd8e16d)
1 /*
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>
18 #include <linux/slab.h>
19 #include <linux/bitrev.h>
20 #include <linux/ratelimit.h>
21 #include <linux/usb.h>
22 #include <linux/usb/audio.h>
23 #include <linux/usb/audio-v2.h>
24 
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"
33 #include "endpoint.h"
34 #include "helper.h"
35 #include "pcm.h"
36 #include "clock.h"
37 #include "power.h"
38 
39 #define SUBSTREAM_FLAG_DATA_EP_STARTED	0
40 #define SUBSTREAM_FLAG_SYNC_EP_STARTED	1
41 
42 /* return the estimated delay based on USB frame counters */
43 snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
44 				    unsigned int rate)
45 {
46 	int current_frame_number;
47 	int frame_diff;
48 	int est_delay;
49 
50 	if (!subs->last_delay)
51 		return 0; /* short path */
52 
53 	current_frame_number = usb_get_current_frame_number(subs->dev);
54 	/*
55 	 * HCD implementations use different widths, use lower 8 bits.
56 	 * The delay will be managed up to 256ms, which is more than
57 	 * enough
58 	 */
59 	frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
60 
61 	/* Approximation based on number of samples per USB frame (ms),
62 	   some truncation for 44.1 but the estimate is good enough */
63 	est_delay =  frame_diff * rate / 1000;
64 	if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
65 		est_delay = subs->last_delay - est_delay;
66 	else
67 		est_delay = subs->last_delay + est_delay;
68 
69 	if (est_delay < 0)
70 		est_delay = 0;
71 	return est_delay;
72 }
73 
74 /*
75  * return the current pcm pointer.  just based on the hwptr_done value.
76  */
77 static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
78 {
79 	struct snd_usb_substream *subs;
80 	unsigned int hwptr_done;
81 
82 	subs = (struct snd_usb_substream *)substream->runtime->private_data;
83 	if (subs->stream->chip->shutdown)
84 		return SNDRV_PCM_POS_XRUN;
85 	spin_lock(&subs->lock);
86 	hwptr_done = subs->hwptr_done;
87 	substream->runtime->delay = snd_usb_pcm_delay(subs,
88 						substream->runtime->rate);
89 	spin_unlock(&subs->lock);
90 	return hwptr_done / (substream->runtime->frame_bits >> 3);
91 }
92 
93 /*
94  * find a matching audio format
95  */
96 static struct audioformat *find_format(struct snd_usb_substream *subs)
97 {
98 	struct audioformat *fp;
99 	struct audioformat *found = NULL;
100 	int cur_attr = 0, attr;
101 
102 	list_for_each_entry(fp, &subs->fmt_list, list) {
103 		if (!(fp->formats & pcm_format_to_bits(subs->pcm_format)))
104 			continue;
105 		if (fp->channels != subs->channels)
106 			continue;
107 		if (subs->cur_rate < fp->rate_min ||
108 		    subs->cur_rate > fp->rate_max)
109 			continue;
110 		if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
111 			unsigned int i;
112 			for (i = 0; i < fp->nr_rates; i++)
113 				if (fp->rate_table[i] == subs->cur_rate)
114 					break;
115 			if (i >= fp->nr_rates)
116 				continue;
117 		}
118 		attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
119 		if (! found) {
120 			found = fp;
121 			cur_attr = attr;
122 			continue;
123 		}
124 		/* avoid async out and adaptive in if the other method
125 		 * supports the same format.
126 		 * this is a workaround for the case like
127 		 * M-audio audiophile USB.
128 		 */
129 		if (attr != cur_attr) {
130 			if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
131 			     subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
132 			    (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
133 			     subs->direction == SNDRV_PCM_STREAM_CAPTURE))
134 				continue;
135 			if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
136 			     subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
137 			    (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
138 			     subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
139 				found = fp;
140 				cur_attr = attr;
141 				continue;
142 			}
143 		}
144 		/* find the format with the largest max. packet size */
145 		if (fp->maxpacksize > found->maxpacksize) {
146 			found = fp;
147 			cur_attr = attr;
148 		}
149 	}
150 	return found;
151 }
152 
153 static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
154 			 struct usb_host_interface *alts,
155 			 struct audioformat *fmt)
156 {
157 	struct usb_device *dev = chip->dev;
158 	unsigned int ep;
159 	unsigned char data[1];
160 	int err;
161 
162 	ep = get_endpoint(alts, 0)->bEndpointAddress;
163 
164 	data[0] = 1;
165 	if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
166 				   USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
167 				   UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
168 				   data, sizeof(data))) < 0) {
169 		snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n",
170 			   dev->devnum, iface, ep);
171 		return err;
172 	}
173 
174 	return 0;
175 }
176 
177 static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
178 			 struct usb_host_interface *alts,
179 			 struct audioformat *fmt)
180 {
181 	struct usb_device *dev = chip->dev;
182 	unsigned char data[1];
183 	int err;
184 
185 	data[0] = 1;
186 	if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
187 				   USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
188 				   UAC2_EP_CS_PITCH << 8, 0,
189 				   data, sizeof(data))) < 0) {
190 		snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n",
191 			   dev->devnum, iface, fmt->altsetting);
192 		return err;
193 	}
194 
195 	return 0;
196 }
197 
198 /*
199  * initialize the pitch control and sample rate
200  */
201 int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
202 		       struct usb_host_interface *alts,
203 		       struct audioformat *fmt)
204 {
205 	/* if endpoint doesn't have pitch control, bail out */
206 	if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
207 		return 0;
208 
209 	switch (fmt->protocol) {
210 	case UAC_VERSION_1:
211 	default:
212 		return init_pitch_v1(chip, iface, alts, fmt);
213 
214 	case UAC_VERSION_2:
215 		return init_pitch_v2(chip, iface, alts, fmt);
216 	}
217 }
218 
219 static int start_endpoints(struct snd_usb_substream *subs, bool can_sleep)
220 {
221 	int err;
222 
223 	if (!subs->data_endpoint)
224 		return -EINVAL;
225 
226 	if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
227 		struct snd_usb_endpoint *ep = subs->data_endpoint;
228 
229 		snd_printdd(KERN_DEBUG "Starting data EP @%p\n", ep);
230 
231 		ep->data_subs = subs;
232 		err = snd_usb_endpoint_start(ep, can_sleep);
233 		if (err < 0) {
234 			clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
235 			return err;
236 		}
237 	}
238 
239 	if (subs->sync_endpoint &&
240 	    !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
241 		struct snd_usb_endpoint *ep = subs->sync_endpoint;
242 
243 		if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
244 		    subs->data_endpoint->alt_idx != subs->sync_endpoint->alt_idx) {
245 			err = usb_set_interface(subs->dev,
246 						subs->sync_endpoint->iface,
247 						subs->sync_endpoint->alt_idx);
248 			if (err < 0) {
249 				snd_printk(KERN_ERR
250 					   "%d:%d:%d: cannot set interface (%d)\n",
251 					   subs->dev->devnum,
252 					   subs->sync_endpoint->iface,
253 					   subs->sync_endpoint->alt_idx, err);
254 				return -EIO;
255 			}
256 		}
257 
258 		snd_printdd(KERN_DEBUG "Starting sync EP @%p\n", ep);
259 
260 		ep->sync_slave = subs->data_endpoint;
261 		err = snd_usb_endpoint_start(ep, can_sleep);
262 		if (err < 0) {
263 			clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
264 			return err;
265 		}
266 	}
267 
268 	return 0;
269 }
270 
271 static void stop_endpoints(struct snd_usb_substream *subs, bool wait)
272 {
273 	if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
274 		snd_usb_endpoint_stop(subs->sync_endpoint);
275 
276 	if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
277 		snd_usb_endpoint_stop(subs->data_endpoint);
278 
279 	if (wait) {
280 		snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
281 		snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
282 	}
283 }
284 
285 static int deactivate_endpoints(struct snd_usb_substream *subs)
286 {
287 	int reta, retb;
288 
289 	reta = snd_usb_endpoint_deactivate(subs->sync_endpoint);
290 	retb = snd_usb_endpoint_deactivate(subs->data_endpoint);
291 
292 	if (reta < 0)
293 		return reta;
294 
295 	if (retb < 0)
296 		return retb;
297 
298 	return 0;
299 }
300 
301 static int search_roland_implicit_fb(struct usb_device *dev, int ifnum,
302 				     unsigned int altsetting,
303 				     struct usb_host_interface **alts,
304 				     unsigned int *ep)
305 {
306 	struct usb_interface *iface;
307 	struct usb_interface_descriptor *altsd;
308 	struct usb_endpoint_descriptor *epd;
309 
310 	iface = usb_ifnum_to_if(dev, ifnum);
311 	if (!iface || iface->num_altsetting < altsetting + 1)
312 		return -ENOENT;
313 	*alts = &iface->altsetting[altsetting];
314 	altsd = get_iface_desc(*alts);
315 	if (altsd->bAlternateSetting != altsetting ||
316 	    altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
317 	    (altsd->bInterfaceSubClass != 2 &&
318 	     altsd->bInterfaceProtocol != 2   ) ||
319 	    altsd->bNumEndpoints < 1)
320 		return -ENOENT;
321 	epd = get_endpoint(*alts, 0);
322 	if (!usb_endpoint_is_isoc_in(epd) ||
323 	    (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
324 					USB_ENDPOINT_USAGE_IMPLICIT_FB)
325 		return -ENOENT;
326 	*ep = epd->bEndpointAddress;
327 	return 0;
328 }
329 
330 static int set_sync_ep_implicit_fb_quirk(struct snd_usb_substream *subs,
331 					 struct usb_device *dev,
332 					 struct usb_interface_descriptor *altsd,
333 					 unsigned int attr)
334 {
335 	struct usb_host_interface *alts;
336 	struct usb_interface *iface;
337 	unsigned int ep;
338 
339 	/* Implicit feedback sync EPs consumers are always playback EPs */
340 	if (subs->direction != SNDRV_PCM_STREAM_PLAYBACK)
341 		return 0;
342 
343 	switch (subs->stream->chip->usb_id) {
344 	case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
345 	case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
346 		ep = 0x81;
347 		iface = usb_ifnum_to_if(dev, 3);
348 
349 		if (!iface || iface->num_altsetting == 0)
350 			return -EINVAL;
351 
352 		alts = &iface->altsetting[1];
353 		goto add_sync_ep;
354 		break;
355 	case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
356 	case USB_ID(0x0763, 0x2081):
357 		ep = 0x81;
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;
365 	}
366 	if (attr == USB_ENDPOINT_SYNC_ASYNC &&
367 	    altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
368 	    altsd->bInterfaceProtocol == 2 &&
369 	    altsd->bNumEndpoints == 1 &&
370 	    USB_ID_VENDOR(subs->stream->chip->usb_id) == 0x0582 /* Roland */ &&
371 	    search_roland_implicit_fb(dev, altsd->bInterfaceNumber + 1,
372 				      altsd->bAlternateSetting,
373 				      &alts, &ep) >= 0) {
374 		goto add_sync_ep;
375 	}
376 
377 	/* No quirk */
378 	return 0;
379 
380 add_sync_ep:
381 	subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
382 						   alts, ep, !subs->direction,
383 						   SND_USB_ENDPOINT_TYPE_DATA);
384 	if (!subs->sync_endpoint)
385 		return -EINVAL;
386 
387 	subs->data_endpoint->sync_master = subs->sync_endpoint;
388 
389 	return 0;
390 }
391 
392 static int set_sync_endpoint(struct snd_usb_substream *subs,
393 			     struct audioformat *fmt,
394 			     struct usb_device *dev,
395 			     struct usb_host_interface *alts,
396 			     struct usb_interface_descriptor *altsd)
397 {
398 	int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
399 	unsigned int ep, attr;
400 	bool implicit_fb;
401 	int err;
402 
403 	/* we need a sync pipe in async OUT or adaptive IN mode */
404 	/* check the number of EP, since some devices have broken
405 	 * descriptors which fool us.  if it has only one EP,
406 	 * assume it as adaptive-out or sync-in.
407 	 */
408 	attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
409 
410 	err = set_sync_ep_implicit_fb_quirk(subs, dev, altsd, attr);
411 	if (err < 0)
412 		return err;
413 
414 	if (altsd->bNumEndpoints < 2)
415 		return 0;
416 
417 	if ((is_playback && attr != USB_ENDPOINT_SYNC_ASYNC) ||
418 	    (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
419 		return 0;
420 
421 	/* check sync-pipe endpoint */
422 	/* ... and check descriptor size before accessing bSynchAddress
423 	   because there is a version of the SB Audigy 2 NX firmware lacking
424 	   the audio fields in the endpoint descriptors */
425 	if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
426 	    (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
427 	     get_endpoint(alts, 1)->bSynchAddress != 0)) {
428 		snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
429 			   dev->devnum, fmt->iface, fmt->altsetting,
430 			   get_endpoint(alts, 1)->bmAttributes,
431 			   get_endpoint(alts, 1)->bLength,
432 			   get_endpoint(alts, 1)->bSynchAddress);
433 		return -EINVAL;
434 	}
435 	ep = get_endpoint(alts, 1)->bEndpointAddress;
436 	if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
437 	    ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
438 	     (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
439 		snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
440 			   dev->devnum, fmt->iface, fmt->altsetting,
441 			   is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
442 		return -EINVAL;
443 	}
444 
445 	implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
446 			== USB_ENDPOINT_USAGE_IMPLICIT_FB;
447 
448 	subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
449 						   alts, ep, !subs->direction,
450 						   implicit_fb ?
451 							SND_USB_ENDPOINT_TYPE_DATA :
452 							SND_USB_ENDPOINT_TYPE_SYNC);
453 	if (!subs->sync_endpoint)
454 		return -EINVAL;
455 
456 	subs->data_endpoint->sync_master = subs->sync_endpoint;
457 
458 	return 0;
459 }
460 
461 /*
462  * find a matching format and set up the interface
463  */
464 static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
465 {
466 	struct usb_device *dev = subs->dev;
467 	struct usb_host_interface *alts;
468 	struct usb_interface_descriptor *altsd;
469 	struct usb_interface *iface;
470 	int err;
471 
472 	iface = usb_ifnum_to_if(dev, fmt->iface);
473 	if (WARN_ON(!iface))
474 		return -EINVAL;
475 	alts = &iface->altsetting[fmt->altset_idx];
476 	altsd = get_iface_desc(alts);
477 	if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting))
478 		return -EINVAL;
479 
480 	if (fmt == subs->cur_audiofmt)
481 		return 0;
482 
483 	/* close the old interface */
484 	if (subs->interface >= 0 && subs->interface != fmt->iface) {
485 		err = usb_set_interface(subs->dev, subs->interface, 0);
486 		if (err < 0) {
487 			snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed (%d)\n",
488 				dev->devnum, fmt->iface, fmt->altsetting, err);
489 			return -EIO;
490 		}
491 		subs->interface = -1;
492 		subs->altset_idx = 0;
493 	}
494 
495 	/* set interface */
496 	if (subs->interface != fmt->iface ||
497 	    subs->altset_idx != fmt->altset_idx) {
498 		err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
499 		if (err < 0) {
500 			snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed (%d)\n",
501 				   dev->devnum, fmt->iface, fmt->altsetting, err);
502 			return -EIO;
503 		}
504 		snd_printdd(KERN_INFO "setting usb interface %d:%d\n",
505 				fmt->iface, fmt->altsetting);
506 		subs->interface = fmt->iface;
507 		subs->altset_idx = fmt->altset_idx;
508 
509 		snd_usb_set_interface_quirk(dev);
510 	}
511 
512 	subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
513 						   alts, fmt->endpoint, subs->direction,
514 						   SND_USB_ENDPOINT_TYPE_DATA);
515 
516 	if (!subs->data_endpoint)
517 		return -EINVAL;
518 
519 	err = set_sync_endpoint(subs, fmt, dev, alts, altsd);
520 	if (err < 0)
521 		return err;
522 
523 	err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
524 	if (err < 0)
525 		return err;
526 
527 	subs->cur_audiofmt = fmt;
528 
529 	snd_usb_set_format_quirk(subs, fmt);
530 
531 	return 0;
532 }
533 
534 /*
535  * Return the score of matching two audioformats.
536  * Veto the audioformat if:
537  * - It has no channels for some reason.
538  * - Requested PCM format is not supported.
539  * - Requested sample rate is not supported.
540  */
541 static int match_endpoint_audioformats(struct audioformat *fp,
542 	struct audioformat *match, int rate,
543 	snd_pcm_format_t pcm_format)
544 {
545 	int i;
546 	int score = 0;
547 
548 	if (fp->channels < 1) {
549 		snd_printdd("%s: (fmt @%p) no channels\n", __func__, fp);
550 		return 0;
551 	}
552 
553 	if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
554 		snd_printdd("%s: (fmt @%p) no match for format %d\n", __func__,
555 			fp, pcm_format);
556 		return 0;
557 	}
558 
559 	for (i = 0; i < fp->nr_rates; i++) {
560 		if (fp->rate_table[i] == rate) {
561 			score++;
562 			break;
563 		}
564 	}
565 	if (!score) {
566 		snd_printdd("%s: (fmt @%p) no match for rate %d\n", __func__,
567 			fp, rate);
568 		return 0;
569 	}
570 
571 	if (fp->channels == match->channels)
572 		score++;
573 
574 	snd_printdd("%s: (fmt @%p) score %d\n", __func__, fp, score);
575 
576 	return score;
577 }
578 
579 /*
580  * Configure the sync ep using the rate and pcm format of the data ep.
581  */
582 static int configure_sync_endpoint(struct snd_usb_substream *subs)
583 {
584 	int ret;
585 	struct audioformat *fp;
586 	struct audioformat *sync_fp = NULL;
587 	int cur_score = 0;
588 	int sync_period_bytes = subs->period_bytes;
589 	struct snd_usb_substream *sync_subs =
590 		&subs->stream->substream[subs->direction ^ 1];
591 
592 	if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
593 	    !subs->stream)
594 		return snd_usb_endpoint_set_params(subs->sync_endpoint,
595 						   subs->pcm_format,
596 						   subs->channels,
597 						   subs->period_bytes,
598 						   subs->cur_rate,
599 						   subs->cur_audiofmt,
600 						   NULL);
601 
602 	/* Try to find the best matching audioformat. */
603 	list_for_each_entry(fp, &sync_subs->fmt_list, list) {
604 		int score = match_endpoint_audioformats(fp, subs->cur_audiofmt,
605 			subs->cur_rate, subs->pcm_format);
606 
607 		if (score > cur_score) {
608 			sync_fp = fp;
609 			cur_score = score;
610 		}
611 	}
612 
613 	if (unlikely(sync_fp == NULL)) {
614 		snd_printk(KERN_ERR "%s: no valid audioformat for sync ep %x found\n",
615 			__func__, sync_subs->ep_num);
616 		return -EINVAL;
617 	}
618 
619 	/*
620 	 * Recalculate the period bytes if channel number differ between
621 	 * data and sync ep audioformat.
622 	 */
623 	if (sync_fp->channels != subs->channels) {
624 		sync_period_bytes = (subs->period_bytes / subs->channels) *
625 			sync_fp->channels;
626 		snd_printdd("%s: adjusted sync ep period bytes (%d -> %d)\n",
627 			__func__, subs->period_bytes, sync_period_bytes);
628 	}
629 
630 	ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
631 					  subs->pcm_format,
632 					  sync_fp->channels,
633 					  sync_period_bytes,
634 					  subs->cur_rate,
635 					  sync_fp,
636 					  NULL);
637 
638 	return ret;
639 }
640 
641 /*
642  * configure endpoint params
643  *
644  * called  during initial setup and upon resume
645  */
646 static int configure_endpoint(struct snd_usb_substream *subs)
647 {
648 	int ret;
649 
650 	/* format changed */
651 	stop_endpoints(subs, true);
652 	ret = snd_usb_endpoint_set_params(subs->data_endpoint,
653 					  subs->pcm_format,
654 					  subs->channels,
655 					  subs->period_bytes,
656 					  subs->cur_rate,
657 					  subs->cur_audiofmt,
658 					  subs->sync_endpoint);
659 	if (ret < 0)
660 		return ret;
661 
662 	if (subs->sync_endpoint)
663 		ret = configure_sync_endpoint(subs);
664 
665 	return ret;
666 }
667 
668 /*
669  * hw_params callback
670  *
671  * allocate a buffer and set the given audio format.
672  *
673  * so far we use a physically linear buffer although packetize transfer
674  * doesn't need a continuous area.
675  * if sg buffer is supported on the later version of alsa, we'll follow
676  * that.
677  */
678 static int snd_usb_hw_params(struct snd_pcm_substream *substream,
679 			     struct snd_pcm_hw_params *hw_params)
680 {
681 	struct snd_usb_substream *subs = substream->runtime->private_data;
682 	struct audioformat *fmt;
683 	int ret;
684 
685 	ret = snd_pcm_lib_alloc_vmalloc_buffer(substream,
686 					       params_buffer_bytes(hw_params));
687 	if (ret < 0)
688 		return ret;
689 
690 	subs->pcm_format = params_format(hw_params);
691 	subs->period_bytes = params_period_bytes(hw_params);
692 	subs->channels = params_channels(hw_params);
693 	subs->cur_rate = params_rate(hw_params);
694 
695 	fmt = find_format(subs);
696 	if (!fmt) {
697 		snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n",
698 			   subs->pcm_format, subs->cur_rate, subs->channels);
699 		return -EINVAL;
700 	}
701 
702 	down_read(&subs->stream->chip->shutdown_rwsem);
703 	if (subs->stream->chip->shutdown)
704 		ret = -ENODEV;
705 	else
706 		ret = set_format(subs, fmt);
707 	up_read(&subs->stream->chip->shutdown_rwsem);
708 	if (ret < 0)
709 		return ret;
710 
711 	subs->interface = fmt->iface;
712 	subs->altset_idx = fmt->altset_idx;
713 	subs->need_setup_ep = true;
714 
715 	return 0;
716 }
717 
718 /*
719  * hw_free callback
720  *
721  * reset the audio format and release the buffer
722  */
723 static int snd_usb_hw_free(struct snd_pcm_substream *substream)
724 {
725 	struct snd_usb_substream *subs = substream->runtime->private_data;
726 
727 	subs->cur_audiofmt = NULL;
728 	subs->cur_rate = 0;
729 	subs->period_bytes = 0;
730 	down_read(&subs->stream->chip->shutdown_rwsem);
731 	if (!subs->stream->chip->shutdown) {
732 		stop_endpoints(subs, true);
733 		deactivate_endpoints(subs);
734 	}
735 	up_read(&subs->stream->chip->shutdown_rwsem);
736 	return snd_pcm_lib_free_vmalloc_buffer(substream);
737 }
738 
739 /*
740  * prepare callback
741  *
742  * only a few subtle things...
743  */
744 static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
745 {
746 	struct snd_pcm_runtime *runtime = substream->runtime;
747 	struct snd_usb_substream *subs = runtime->private_data;
748 	struct usb_host_interface *alts;
749 	struct usb_interface *iface;
750 	int ret;
751 
752 	if (! subs->cur_audiofmt) {
753 		snd_printk(KERN_ERR "usbaudio: no format is specified!\n");
754 		return -ENXIO;
755 	}
756 
757 	down_read(&subs->stream->chip->shutdown_rwsem);
758 	if (subs->stream->chip->shutdown) {
759 		ret = -ENODEV;
760 		goto unlock;
761 	}
762 	if (snd_BUG_ON(!subs->data_endpoint)) {
763 		ret = -EIO;
764 		goto unlock;
765 	}
766 
767 	snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
768 	snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
769 
770 	ret = set_format(subs, subs->cur_audiofmt);
771 	if (ret < 0)
772 		goto unlock;
773 
774 	iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
775 	alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
776 	ret = snd_usb_init_sample_rate(subs->stream->chip,
777 				       subs->cur_audiofmt->iface,
778 				       alts,
779 				       subs->cur_audiofmt,
780 				       subs->cur_rate);
781 	if (ret < 0)
782 		goto unlock;
783 
784 	if (subs->need_setup_ep) {
785 		ret = configure_endpoint(subs);
786 		if (ret < 0)
787 			goto unlock;
788 		subs->need_setup_ep = false;
789 	}
790 
791 	/* some unit conversions in runtime */
792 	subs->data_endpoint->maxframesize =
793 		bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
794 	subs->data_endpoint->curframesize =
795 		bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
796 
797 	/* reset the pointer */
798 	subs->hwptr_done = 0;
799 	subs->transfer_done = 0;
800 	subs->last_delay = 0;
801 	subs->last_frame_number = 0;
802 	runtime->delay = 0;
803 
804 	/* for playback, submit the URBs now; otherwise, the first hwptr_done
805 	 * updates for all URBs would happen at the same time when starting */
806 	if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
807 		ret = start_endpoints(subs, true);
808 
809  unlock:
810 	up_read(&subs->stream->chip->shutdown_rwsem);
811 	return ret;
812 }
813 
814 static struct snd_pcm_hardware snd_usb_hardware =
815 {
816 	.info =			SNDRV_PCM_INFO_MMAP |
817 				SNDRV_PCM_INFO_MMAP_VALID |
818 				SNDRV_PCM_INFO_BATCH |
819 				SNDRV_PCM_INFO_INTERLEAVED |
820 				SNDRV_PCM_INFO_BLOCK_TRANSFER |
821 				SNDRV_PCM_INFO_PAUSE,
822 	.buffer_bytes_max =	1024 * 1024,
823 	.period_bytes_min =	64,
824 	.period_bytes_max =	512 * 1024,
825 	.periods_min =		2,
826 	.periods_max =		1024,
827 };
828 
829 static int hw_check_valid_format(struct snd_usb_substream *subs,
830 				 struct snd_pcm_hw_params *params,
831 				 struct audioformat *fp)
832 {
833 	struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
834 	struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
835 	struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
836 	struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
837 	struct snd_mask check_fmts;
838 	unsigned int ptime;
839 
840 	/* check the format */
841 	snd_mask_none(&check_fmts);
842 	check_fmts.bits[0] = (u32)fp->formats;
843 	check_fmts.bits[1] = (u32)(fp->formats >> 32);
844 	snd_mask_intersect(&check_fmts, fmts);
845 	if (snd_mask_empty(&check_fmts)) {
846 		hwc_debug("   > check: no supported format %d\n", fp->format);
847 		return 0;
848 	}
849 	/* check the channels */
850 	if (fp->channels < ct->min || fp->channels > ct->max) {
851 		hwc_debug("   > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
852 		return 0;
853 	}
854 	/* check the rate is within the range */
855 	if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
856 		hwc_debug("   > check: rate_min %d > max %d\n", fp->rate_min, it->max);
857 		return 0;
858 	}
859 	if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
860 		hwc_debug("   > check: rate_max %d < min %d\n", fp->rate_max, it->min);
861 		return 0;
862 	}
863 	/* check whether the period time is >= the data packet interval */
864 	if (subs->speed != USB_SPEED_FULL) {
865 		ptime = 125 * (1 << fp->datainterval);
866 		if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
867 			hwc_debug("   > check: ptime %u > max %u\n", ptime, pt->max);
868 			return 0;
869 		}
870 	}
871 	return 1;
872 }
873 
874 static int hw_rule_rate(struct snd_pcm_hw_params *params,
875 			struct snd_pcm_hw_rule *rule)
876 {
877 	struct snd_usb_substream *subs = rule->private;
878 	struct audioformat *fp;
879 	struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
880 	unsigned int rmin, rmax;
881 	int changed;
882 
883 	hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
884 	changed = 0;
885 	rmin = rmax = 0;
886 	list_for_each_entry(fp, &subs->fmt_list, list) {
887 		if (!hw_check_valid_format(subs, params, fp))
888 			continue;
889 		if (changed++) {
890 			if (rmin > fp->rate_min)
891 				rmin = fp->rate_min;
892 			if (rmax < fp->rate_max)
893 				rmax = fp->rate_max;
894 		} else {
895 			rmin = fp->rate_min;
896 			rmax = fp->rate_max;
897 		}
898 	}
899 
900 	if (!changed) {
901 		hwc_debug("  --> get empty\n");
902 		it->empty = 1;
903 		return -EINVAL;
904 	}
905 
906 	changed = 0;
907 	if (it->min < rmin) {
908 		it->min = rmin;
909 		it->openmin = 0;
910 		changed = 1;
911 	}
912 	if (it->max > rmax) {
913 		it->max = rmax;
914 		it->openmax = 0;
915 		changed = 1;
916 	}
917 	if (snd_interval_checkempty(it)) {
918 		it->empty = 1;
919 		return -EINVAL;
920 	}
921 	hwc_debug("  --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
922 	return changed;
923 }
924 
925 
926 static int hw_rule_channels(struct snd_pcm_hw_params *params,
927 			    struct snd_pcm_hw_rule *rule)
928 {
929 	struct snd_usb_substream *subs = rule->private;
930 	struct audioformat *fp;
931 	struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
932 	unsigned int rmin, rmax;
933 	int changed;
934 
935 	hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
936 	changed = 0;
937 	rmin = rmax = 0;
938 	list_for_each_entry(fp, &subs->fmt_list, list) {
939 		if (!hw_check_valid_format(subs, params, fp))
940 			continue;
941 		if (changed++) {
942 			if (rmin > fp->channels)
943 				rmin = fp->channels;
944 			if (rmax < fp->channels)
945 				rmax = fp->channels;
946 		} else {
947 			rmin = fp->channels;
948 			rmax = fp->channels;
949 		}
950 	}
951 
952 	if (!changed) {
953 		hwc_debug("  --> get empty\n");
954 		it->empty = 1;
955 		return -EINVAL;
956 	}
957 
958 	changed = 0;
959 	if (it->min < rmin) {
960 		it->min = rmin;
961 		it->openmin = 0;
962 		changed = 1;
963 	}
964 	if (it->max > rmax) {
965 		it->max = rmax;
966 		it->openmax = 0;
967 		changed = 1;
968 	}
969 	if (snd_interval_checkempty(it)) {
970 		it->empty = 1;
971 		return -EINVAL;
972 	}
973 	hwc_debug("  --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
974 	return changed;
975 }
976 
977 static int hw_rule_format(struct snd_pcm_hw_params *params,
978 			  struct snd_pcm_hw_rule *rule)
979 {
980 	struct snd_usb_substream *subs = rule->private;
981 	struct audioformat *fp;
982 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
983 	u64 fbits;
984 	u32 oldbits[2];
985 	int changed;
986 
987 	hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
988 	fbits = 0;
989 	list_for_each_entry(fp, &subs->fmt_list, list) {
990 		if (!hw_check_valid_format(subs, params, fp))
991 			continue;
992 		fbits |= fp->formats;
993 	}
994 
995 	oldbits[0] = fmt->bits[0];
996 	oldbits[1] = fmt->bits[1];
997 	fmt->bits[0] &= (u32)fbits;
998 	fmt->bits[1] &= (u32)(fbits >> 32);
999 	if (!fmt->bits[0] && !fmt->bits[1]) {
1000 		hwc_debug("  --> get empty\n");
1001 		return -EINVAL;
1002 	}
1003 	changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
1004 	hwc_debug("  --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
1005 	return changed;
1006 }
1007 
1008 static int hw_rule_period_time(struct snd_pcm_hw_params *params,
1009 			       struct snd_pcm_hw_rule *rule)
1010 {
1011 	struct snd_usb_substream *subs = rule->private;
1012 	struct audioformat *fp;
1013 	struct snd_interval *it;
1014 	unsigned char min_datainterval;
1015 	unsigned int pmin;
1016 	int changed;
1017 
1018 	it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
1019 	hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
1020 	min_datainterval = 0xff;
1021 	list_for_each_entry(fp, &subs->fmt_list, list) {
1022 		if (!hw_check_valid_format(subs, params, fp))
1023 			continue;
1024 		min_datainterval = min(min_datainterval, fp->datainterval);
1025 	}
1026 	if (min_datainterval == 0xff) {
1027 		hwc_debug("  --> get empty\n");
1028 		it->empty = 1;
1029 		return -EINVAL;
1030 	}
1031 	pmin = 125 * (1 << min_datainterval);
1032 	changed = 0;
1033 	if (it->min < pmin) {
1034 		it->min = pmin;
1035 		it->openmin = 0;
1036 		changed = 1;
1037 	}
1038 	if (snd_interval_checkempty(it)) {
1039 		it->empty = 1;
1040 		return -EINVAL;
1041 	}
1042 	hwc_debug("  --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
1043 	return changed;
1044 }
1045 
1046 /*
1047  *  If the device supports unusual bit rates, does the request meet these?
1048  */
1049 static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
1050 				  struct snd_usb_substream *subs)
1051 {
1052 	struct audioformat *fp;
1053 	int *rate_list;
1054 	int count = 0, needs_knot = 0;
1055 	int err;
1056 
1057 	kfree(subs->rate_list.list);
1058 	subs->rate_list.list = NULL;
1059 
1060 	list_for_each_entry(fp, &subs->fmt_list, list) {
1061 		if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
1062 			return 0;
1063 		count += fp->nr_rates;
1064 		if (fp->rates & SNDRV_PCM_RATE_KNOT)
1065 			needs_knot = 1;
1066 	}
1067 	if (!needs_knot)
1068 		return 0;
1069 
1070 	subs->rate_list.list = rate_list =
1071 		kmalloc(sizeof(int) * count, GFP_KERNEL);
1072 	if (!subs->rate_list.list)
1073 		return -ENOMEM;
1074 	subs->rate_list.count = count;
1075 	subs->rate_list.mask = 0;
1076 	count = 0;
1077 	list_for_each_entry(fp, &subs->fmt_list, list) {
1078 		int i;
1079 		for (i = 0; i < fp->nr_rates; i++)
1080 			rate_list[count++] = fp->rate_table[i];
1081 	}
1082 	err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1083 					 &subs->rate_list);
1084 	if (err < 0)
1085 		return err;
1086 
1087 	return 0;
1088 }
1089 
1090 
1091 /*
1092  * set up the runtime hardware information.
1093  */
1094 
1095 static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
1096 {
1097 	struct audioformat *fp;
1098 	unsigned int pt, ptmin;
1099 	int param_period_time_if_needed;
1100 	int err;
1101 
1102 	runtime->hw.formats = subs->formats;
1103 
1104 	runtime->hw.rate_min = 0x7fffffff;
1105 	runtime->hw.rate_max = 0;
1106 	runtime->hw.channels_min = 256;
1107 	runtime->hw.channels_max = 0;
1108 	runtime->hw.rates = 0;
1109 	ptmin = UINT_MAX;
1110 	/* check min/max rates and channels */
1111 	list_for_each_entry(fp, &subs->fmt_list, list) {
1112 		runtime->hw.rates |= fp->rates;
1113 		if (runtime->hw.rate_min > fp->rate_min)
1114 			runtime->hw.rate_min = fp->rate_min;
1115 		if (runtime->hw.rate_max < fp->rate_max)
1116 			runtime->hw.rate_max = fp->rate_max;
1117 		if (runtime->hw.channels_min > fp->channels)
1118 			runtime->hw.channels_min = fp->channels;
1119 		if (runtime->hw.channels_max < fp->channels)
1120 			runtime->hw.channels_max = fp->channels;
1121 		if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
1122 			/* FIXME: there might be more than one audio formats... */
1123 			runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
1124 				fp->frame_size;
1125 		}
1126 		pt = 125 * (1 << fp->datainterval);
1127 		ptmin = min(ptmin, pt);
1128 	}
1129 	err = snd_usb_autoresume(subs->stream->chip);
1130 	if (err < 0)
1131 		return err;
1132 
1133 	param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
1134 	if (subs->speed == USB_SPEED_FULL)
1135 		/* full speed devices have fixed data packet interval */
1136 		ptmin = 1000;
1137 	if (ptmin == 1000)
1138 		/* if period time doesn't go below 1 ms, no rules needed */
1139 		param_period_time_if_needed = -1;
1140 	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1141 				     ptmin, UINT_MAX);
1142 
1143 	if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1144 				       hw_rule_rate, subs,
1145 				       SNDRV_PCM_HW_PARAM_FORMAT,
1146 				       SNDRV_PCM_HW_PARAM_CHANNELS,
1147 				       param_period_time_if_needed,
1148 				       -1)) < 0)
1149 		goto rep_err;
1150 	if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1151 				       hw_rule_channels, subs,
1152 				       SNDRV_PCM_HW_PARAM_FORMAT,
1153 				       SNDRV_PCM_HW_PARAM_RATE,
1154 				       param_period_time_if_needed,
1155 				       -1)) < 0)
1156 		goto rep_err;
1157 	if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1158 				       hw_rule_format, subs,
1159 				       SNDRV_PCM_HW_PARAM_RATE,
1160 				       SNDRV_PCM_HW_PARAM_CHANNELS,
1161 				       param_period_time_if_needed,
1162 				       -1)) < 0)
1163 		goto rep_err;
1164 	if (param_period_time_if_needed >= 0) {
1165 		err = snd_pcm_hw_rule_add(runtime, 0,
1166 					  SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1167 					  hw_rule_period_time, subs,
1168 					  SNDRV_PCM_HW_PARAM_FORMAT,
1169 					  SNDRV_PCM_HW_PARAM_CHANNELS,
1170 					  SNDRV_PCM_HW_PARAM_RATE,
1171 					  -1);
1172 		if (err < 0)
1173 			goto rep_err;
1174 	}
1175 	if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0)
1176 		goto rep_err;
1177 	return 0;
1178 
1179 rep_err:
1180 	snd_usb_autosuspend(subs->stream->chip);
1181 	return err;
1182 }
1183 
1184 static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction)
1185 {
1186 	struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1187 	struct snd_pcm_runtime *runtime = substream->runtime;
1188 	struct snd_usb_substream *subs = &as->substream[direction];
1189 
1190 	subs->interface = -1;
1191 	subs->altset_idx = 0;
1192 	runtime->hw = snd_usb_hardware;
1193 	runtime->private_data = subs;
1194 	subs->pcm_substream = substream;
1195 	/* runtime PM is also done there */
1196 
1197 	/* initialize DSD/DOP context */
1198 	subs->dsd_dop.byte_idx = 0;
1199 	subs->dsd_dop.channel = 0;
1200 	subs->dsd_dop.marker = 1;
1201 
1202 	return setup_hw_info(runtime, subs);
1203 }
1204 
1205 static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction)
1206 {
1207 	struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
1208 	struct snd_usb_substream *subs = &as->substream[direction];
1209 
1210 	stop_endpoints(subs, true);
1211 
1212 	if (!as->chip->shutdown && subs->interface >= 0) {
1213 		usb_set_interface(subs->dev, subs->interface, 0);
1214 		subs->interface = -1;
1215 	}
1216 
1217 	subs->pcm_substream = NULL;
1218 	snd_usb_autosuspend(subs->stream->chip);
1219 
1220 	return 0;
1221 }
1222 
1223 /* Since a URB can handle only a single linear buffer, we must use double
1224  * buffering when the data to be transferred overflows the buffer boundary.
1225  * To avoid inconsistencies when updating hwptr_done, we use double buffering
1226  * for all URBs.
1227  */
1228 static void retire_capture_urb(struct snd_usb_substream *subs,
1229 			       struct urb *urb)
1230 {
1231 	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1232 	unsigned int stride, frames, bytes, oldptr;
1233 	int i, period_elapsed = 0;
1234 	unsigned long flags;
1235 	unsigned char *cp;
1236 	int current_frame_number;
1237 
1238 	/* read frame number here, update pointer in critical section */
1239 	current_frame_number = usb_get_current_frame_number(subs->dev);
1240 
1241 	stride = runtime->frame_bits >> 3;
1242 
1243 	for (i = 0; i < urb->number_of_packets; i++) {
1244 		cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
1245 		if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
1246 			snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status);
1247 			// continue;
1248 		}
1249 		bytes = urb->iso_frame_desc[i].actual_length;
1250 		frames = bytes / stride;
1251 		if (!subs->txfr_quirk)
1252 			bytes = frames * stride;
1253 		if (bytes % (runtime->sample_bits >> 3) != 0) {
1254 			int oldbytes = bytes;
1255 			bytes = frames * stride;
1256 			snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n",
1257 							oldbytes, bytes);
1258 		}
1259 		/* update the current pointer */
1260 		spin_lock_irqsave(&subs->lock, flags);
1261 		oldptr = subs->hwptr_done;
1262 		subs->hwptr_done += bytes;
1263 		if (subs->hwptr_done >= runtime->buffer_size * stride)
1264 			subs->hwptr_done -= runtime->buffer_size * stride;
1265 		frames = (bytes + (oldptr % stride)) / stride;
1266 		subs->transfer_done += frames;
1267 		if (subs->transfer_done >= runtime->period_size) {
1268 			subs->transfer_done -= runtime->period_size;
1269 			period_elapsed = 1;
1270 		}
1271 		/* capture delay is by construction limited to one URB,
1272 		 * reset delays here
1273 		 */
1274 		runtime->delay = subs->last_delay = 0;
1275 
1276 		/* realign last_frame_number */
1277 		subs->last_frame_number = current_frame_number;
1278 		subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1279 
1280 		spin_unlock_irqrestore(&subs->lock, flags);
1281 		/* copy a data chunk */
1282 		if (oldptr + bytes > runtime->buffer_size * stride) {
1283 			unsigned int bytes1 =
1284 					runtime->buffer_size * stride - oldptr;
1285 			memcpy(runtime->dma_area + oldptr, cp, bytes1);
1286 			memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
1287 		} else {
1288 			memcpy(runtime->dma_area + oldptr, cp, bytes);
1289 		}
1290 	}
1291 
1292 	if (period_elapsed)
1293 		snd_pcm_period_elapsed(subs->pcm_substream);
1294 }
1295 
1296 static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
1297 					     struct urb *urb, unsigned int bytes)
1298 {
1299 	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1300 	unsigned int stride = runtime->frame_bits >> 3;
1301 	unsigned int dst_idx = 0;
1302 	unsigned int src_idx = subs->hwptr_done;
1303 	unsigned int wrap = runtime->buffer_size * stride;
1304 	u8 *dst = urb->transfer_buffer;
1305 	u8 *src = runtime->dma_area;
1306 	u8 marker[] = { 0x05, 0xfa };
1307 
1308 	/*
1309 	 * The DSP DOP format defines a way to transport DSD samples over
1310 	 * normal PCM data endpoints. It requires stuffing of marker bytes
1311 	 * (0x05 and 0xfa, alternating per sample frame), and then expects
1312 	 * 2 additional bytes of actual payload. The whole frame is stored
1313 	 * LSB.
1314 	 *
1315 	 * Hence, for a stereo transport, the buffer layout looks like this,
1316 	 * where L refers to left channel samples and R to right.
1317 	 *
1318 	 *   L1 L2 0x05   R1 R2 0x05   L3 L4 0xfa  R3 R4 0xfa
1319 	 *   L5 L6 0x05   R5 R6 0x05   L7 L8 0xfa  R7 R8 0xfa
1320 	 *   .....
1321 	 *
1322 	 */
1323 
1324 	while (bytes--) {
1325 		if (++subs->dsd_dop.byte_idx == 3) {
1326 			/* frame boundary? */
1327 			dst[dst_idx++] = marker[subs->dsd_dop.marker];
1328 			src_idx += 2;
1329 			subs->dsd_dop.byte_idx = 0;
1330 
1331 			if (++subs->dsd_dop.channel % runtime->channels == 0) {
1332 				/* alternate the marker */
1333 				subs->dsd_dop.marker++;
1334 				subs->dsd_dop.marker %= ARRAY_SIZE(marker);
1335 				subs->dsd_dop.channel = 0;
1336 			}
1337 		} else {
1338 			/* stuff the DSD payload */
1339 			int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
1340 
1341 			if (subs->cur_audiofmt->dsd_bitrev)
1342 				dst[dst_idx++] = bitrev8(src[idx]);
1343 			else
1344 				dst[dst_idx++] = src[idx];
1345 
1346 			subs->hwptr_done++;
1347 		}
1348 	}
1349 }
1350 
1351 static void prepare_playback_urb(struct snd_usb_substream *subs,
1352 				 struct urb *urb)
1353 {
1354 	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1355 	struct snd_usb_endpoint *ep = subs->data_endpoint;
1356 	struct snd_urb_ctx *ctx = urb->context;
1357 	unsigned int counts, frames, bytes;
1358 	int i, stride, period_elapsed = 0;
1359 	unsigned long flags;
1360 
1361 	stride = runtime->frame_bits >> 3;
1362 
1363 	frames = 0;
1364 	urb->number_of_packets = 0;
1365 	spin_lock_irqsave(&subs->lock, flags);
1366 	for (i = 0; i < ctx->packets; i++) {
1367 		if (ctx->packet_size[i])
1368 			counts = ctx->packet_size[i];
1369 		else
1370 			counts = snd_usb_endpoint_next_packet_size(ep);
1371 
1372 		/* set up descriptor */
1373 		urb->iso_frame_desc[i].offset = frames * ep->stride;
1374 		urb->iso_frame_desc[i].length = counts * ep->stride;
1375 		frames += counts;
1376 		urb->number_of_packets++;
1377 		subs->transfer_done += counts;
1378 		if (subs->transfer_done >= runtime->period_size) {
1379 			subs->transfer_done -= runtime->period_size;
1380 			period_elapsed = 1;
1381 			if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
1382 				if (subs->transfer_done > 0) {
1383 					/* FIXME: fill-max mode is not
1384 					 * supported yet */
1385 					frames -= subs->transfer_done;
1386 					counts -= subs->transfer_done;
1387 					urb->iso_frame_desc[i].length =
1388 						counts * ep->stride;
1389 					subs->transfer_done = 0;
1390 				}
1391 				i++;
1392 				if (i < ctx->packets) {
1393 					/* add a transfer delimiter */
1394 					urb->iso_frame_desc[i].offset =
1395 						frames * ep->stride;
1396 					urb->iso_frame_desc[i].length = 0;
1397 					urb->number_of_packets++;
1398 				}
1399 				break;
1400 			}
1401 		}
1402 		if (period_elapsed &&
1403 		    !snd_usb_endpoint_implicit_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */
1404 			break;
1405 	}
1406 	bytes = frames * ep->stride;
1407 
1408 	if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
1409 		     subs->cur_audiofmt->dsd_dop)) {
1410 		fill_playback_urb_dsd_dop(subs, urb, bytes);
1411 	} else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
1412 			   subs->cur_audiofmt->dsd_bitrev)) {
1413 		/* bit-reverse the bytes */
1414 		u8 *buf = urb->transfer_buffer;
1415 		for (i = 0; i < bytes; i++) {
1416 			int idx = (subs->hwptr_done + i)
1417 				% (runtime->buffer_size * stride);
1418 			buf[i] = bitrev8(runtime->dma_area[idx]);
1419 		}
1420 
1421 		subs->hwptr_done += bytes;
1422 	} else {
1423 		/* usual PCM */
1424 		if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
1425 			/* err, the transferred area goes over buffer boundary. */
1426 			unsigned int bytes1 =
1427 				runtime->buffer_size * stride - subs->hwptr_done;
1428 			memcpy(urb->transfer_buffer,
1429 			       runtime->dma_area + subs->hwptr_done, bytes1);
1430 			memcpy(urb->transfer_buffer + bytes1,
1431 			       runtime->dma_area, bytes - bytes1);
1432 		} else {
1433 			memcpy(urb->transfer_buffer,
1434 			       runtime->dma_area + subs->hwptr_done, bytes);
1435 		}
1436 
1437 		subs->hwptr_done += bytes;
1438 	}
1439 
1440 	if (subs->hwptr_done >= runtime->buffer_size * stride)
1441 		subs->hwptr_done -= runtime->buffer_size * stride;
1442 
1443 	/* update delay with exact number of samples queued */
1444 	runtime->delay = subs->last_delay;
1445 	runtime->delay += frames;
1446 	subs->last_delay = runtime->delay;
1447 
1448 	/* realign last_frame_number */
1449 	subs->last_frame_number = usb_get_current_frame_number(subs->dev);
1450 	subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
1451 
1452 	spin_unlock_irqrestore(&subs->lock, flags);
1453 	urb->transfer_buffer_length = bytes;
1454 	if (period_elapsed)
1455 		snd_pcm_period_elapsed(subs->pcm_substream);
1456 }
1457 
1458 /*
1459  * process after playback data complete
1460  * - decrease the delay count again
1461  */
1462 static void retire_playback_urb(struct snd_usb_substream *subs,
1463 			       struct urb *urb)
1464 {
1465 	unsigned long flags;
1466 	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1467 	struct snd_usb_endpoint *ep = subs->data_endpoint;
1468 	int processed = urb->transfer_buffer_length / ep->stride;
1469 	int est_delay;
1470 
1471 	/* ignore the delay accounting when procssed=0 is given, i.e.
1472 	 * silent payloads are procssed before handling the actual data
1473 	 */
1474 	if (!processed)
1475 		return;
1476 
1477 	spin_lock_irqsave(&subs->lock, flags);
1478 	if (!subs->last_delay)
1479 		goto out; /* short path */
1480 
1481 	est_delay = snd_usb_pcm_delay(subs, runtime->rate);
1482 	/* update delay with exact number of samples played */
1483 	if (processed > subs->last_delay)
1484 		subs->last_delay = 0;
1485 	else
1486 		subs->last_delay -= processed;
1487 	runtime->delay = subs->last_delay;
1488 
1489 	/*
1490 	 * Report when delay estimate is off by more than 2ms.
1491 	 * The error should be lower than 2ms since the estimate relies
1492 	 * on two reads of a counter updated every ms.
1493 	 */
1494 	if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
1495 		snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n",
1496 			est_delay, subs->last_delay);
1497 
1498 	if (!subs->running) {
1499 		/* update last_frame_number for delay counting here since
1500 		 * prepare_playback_urb won't be called during pause
1501 		 */
1502 		subs->last_frame_number =
1503 			usb_get_current_frame_number(subs->dev) & 0xff;
1504 	}
1505 
1506  out:
1507 	spin_unlock_irqrestore(&subs->lock, flags);
1508 }
1509 
1510 static int snd_usb_playback_open(struct snd_pcm_substream *substream)
1511 {
1512 	return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK);
1513 }
1514 
1515 static int snd_usb_playback_close(struct snd_pcm_substream *substream)
1516 {
1517 	return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK);
1518 }
1519 
1520 static int snd_usb_capture_open(struct snd_pcm_substream *substream)
1521 {
1522 	return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE);
1523 }
1524 
1525 static int snd_usb_capture_close(struct snd_pcm_substream *substream)
1526 {
1527 	return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE);
1528 }
1529 
1530 static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
1531 					      int cmd)
1532 {
1533 	struct snd_usb_substream *subs = substream->runtime->private_data;
1534 
1535 	switch (cmd) {
1536 	case SNDRV_PCM_TRIGGER_START:
1537 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1538 		subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
1539 		subs->data_endpoint->retire_data_urb = retire_playback_urb;
1540 		subs->running = 1;
1541 		return 0;
1542 	case SNDRV_PCM_TRIGGER_STOP:
1543 		stop_endpoints(subs, false);
1544 		subs->running = 0;
1545 		return 0;
1546 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1547 		subs->data_endpoint->prepare_data_urb = NULL;
1548 		/* keep retire_data_urb for delay calculation */
1549 		subs->data_endpoint->retire_data_urb = retire_playback_urb;
1550 		subs->running = 0;
1551 		return 0;
1552 	}
1553 
1554 	return -EINVAL;
1555 }
1556 
1557 static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
1558 					     int cmd)
1559 {
1560 	int err;
1561 	struct snd_usb_substream *subs = substream->runtime->private_data;
1562 
1563 	switch (cmd) {
1564 	case SNDRV_PCM_TRIGGER_START:
1565 		err = start_endpoints(subs, false);
1566 		if (err < 0)
1567 			return err;
1568 
1569 		subs->data_endpoint->retire_data_urb = retire_capture_urb;
1570 		subs->running = 1;
1571 		return 0;
1572 	case SNDRV_PCM_TRIGGER_STOP:
1573 		stop_endpoints(subs, false);
1574 		subs->running = 0;
1575 		return 0;
1576 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1577 		subs->data_endpoint->retire_data_urb = NULL;
1578 		subs->running = 0;
1579 		return 0;
1580 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1581 		subs->data_endpoint->retire_data_urb = retire_capture_urb;
1582 		subs->running = 1;
1583 		return 0;
1584 	}
1585 
1586 	return -EINVAL;
1587 }
1588 
1589 static struct snd_pcm_ops snd_usb_playback_ops = {
1590 	.open =		snd_usb_playback_open,
1591 	.close =	snd_usb_playback_close,
1592 	.ioctl =	snd_pcm_lib_ioctl,
1593 	.hw_params =	snd_usb_hw_params,
1594 	.hw_free =	snd_usb_hw_free,
1595 	.prepare =	snd_usb_pcm_prepare,
1596 	.trigger =	snd_usb_substream_playback_trigger,
1597 	.pointer =	snd_usb_pcm_pointer,
1598 	.page =		snd_pcm_lib_get_vmalloc_page,
1599 	.mmap =		snd_pcm_lib_mmap_vmalloc,
1600 };
1601 
1602 static struct snd_pcm_ops snd_usb_capture_ops = {
1603 	.open =		snd_usb_capture_open,
1604 	.close =	snd_usb_capture_close,
1605 	.ioctl =	snd_pcm_lib_ioctl,
1606 	.hw_params =	snd_usb_hw_params,
1607 	.hw_free =	snd_usb_hw_free,
1608 	.prepare =	snd_usb_pcm_prepare,
1609 	.trigger =	snd_usb_substream_capture_trigger,
1610 	.pointer =	snd_usb_pcm_pointer,
1611 	.page =		snd_pcm_lib_get_vmalloc_page,
1612 	.mmap =		snd_pcm_lib_mmap_vmalloc,
1613 };
1614 
1615 void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
1616 {
1617 	snd_pcm_set_ops(pcm, stream,
1618 			stream == SNDRV_PCM_STREAM_PLAYBACK ?
1619 			&snd_usb_playback_ops : &snd_usb_capture_ops);
1620 }
1621