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