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