Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include <linux/bitrev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/ratelimit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/usb/audio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/usb/audio-v2.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <sound/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <sound/pcm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <sound/pcm_params.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <trace/hooks/sound.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include "usbaudio.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include "card.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include "quirks.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include "endpoint.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include "helper.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include "pcm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include "clock.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include "power.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include "media.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #define SUBSTREAM_FLAG_DATA_EP_STARTED	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #define SUBSTREAM_FLAG_SYNC_EP_STARTED	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) /* return the estimated delay based on USB frame counters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 				    unsigned int rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 	int current_frame_number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 	int frame_diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	int est_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 	if (!subs->last_delay)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 		return 0; /* short path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 	current_frame_number = usb_get_current_frame_number(subs->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	 * HCD implementations use different widths, use lower 8 bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	 * The delay will be managed up to 256ms, which is more than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	 * enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	frame_diff = (current_frame_number - subs->last_frame_number) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	/* Approximation based on number of samples per USB frame (ms),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	   some truncation for 44.1 but the estimate is good enough */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	est_delay =  frame_diff * rate / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 		est_delay = subs->last_delay - est_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 		est_delay = subs->last_delay + est_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	if (est_delay < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 		est_delay = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	return est_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66)  * return the current pcm pointer.  just based on the hwptr_done value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	struct snd_usb_substream *subs = substream->runtime->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	unsigned int hwptr_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	if (atomic_read(&subs->stream->chip->shutdown))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 		return SNDRV_PCM_POS_XRUN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	spin_lock(&subs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	hwptr_done = subs->hwptr_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	substream->runtime->delay = snd_usb_pcm_delay(subs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 						substream->runtime->rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	spin_unlock(&subs->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	return hwptr_done / (substream->runtime->frame_bits >> 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84)  * find a matching audio format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) static struct audioformat *find_format(struct snd_usb_substream *subs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	struct audioformat *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	struct audioformat *found = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	int cur_attr = 0, attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	list_for_each_entry(fp, &subs->fmt_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 		if (!(fp->formats & pcm_format_to_bits(subs->pcm_format)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 		if (fp->channels != subs->channels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 		if (subs->cur_rate < fp->rate_min ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 		    subs->cur_rate > fp->rate_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 		if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 			unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 			for (i = 0; i < fp->nr_rates; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 				if (fp->rate_table[i] == subs->cur_rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 			if (i >= fp->nr_rates)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 		attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 		if (! found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 			found = fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 			cur_attr = attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 		/* avoid async out and adaptive in if the other method
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 		 * supports the same format.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 		 * this is a workaround for the case like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 		 * M-audio audiophile USB.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 		if (attr != cur_attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 			if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 			     subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 			    (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 			     subs->direction == SNDRV_PCM_STREAM_CAPTURE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 			if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 			     subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 			    (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 			     subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 				found = fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 				cur_attr = attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 		/* find the format with the largest max. packet size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 		if (fp->maxpacksize > found->maxpacksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 			found = fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 			cur_attr = attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 		snd_vendor_set_pcm_binterval(fp, found, &cur_attr, &attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	return found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146)  * find a matching audio format as well as non-zero service interval
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) static struct audioformat *find_format_and_si(struct snd_usb_substream *subs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	unsigned int datainterval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	struct audioformat *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	struct audioformat *found = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	int cur_attr = 0, attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	list_for_each_entry(fp, &subs->fmt_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 		if (datainterval != fp->datainterval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 		if (!(fp->formats & pcm_format_to_bits(subs->pcm_format)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 		if (fp->channels != subs->channels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 		if (subs->cur_rate < fp->rate_min ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 		    subs->cur_rate > fp->rate_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 		if (!(fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 			for (i = 0; i < fp->nr_rates; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 				if (fp->rate_table[i] == subs->cur_rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 			if (i >= fp->nr_rates)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 		attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 		if (!found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 			found = fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 			cur_attr = attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 		/* avoid async out and adaptive in if the other method
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 		 * supports the same format.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 		 * this is a workaround for the case like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		 * M-audio audiophile USB.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 		if (attr != cur_attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 			if ((attr == USB_ENDPOINT_SYNC_ASYNC &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 			     subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 			    (attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 			     subs->direction == SNDRV_PCM_STREAM_CAPTURE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 			if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 			     subs->direction == SNDRV_PCM_STREAM_PLAYBACK) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 			    (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 			     subs->direction == SNDRV_PCM_STREAM_CAPTURE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 				found = fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 				cur_attr = attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		/* find the format with the largest max. packet size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		if (fp->maxpacksize > found->maxpacksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 			found = fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 			cur_attr = attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	return found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) static int init_pitch_v1(struct snd_usb_audio *chip, int iface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 			 struct usb_host_interface *alts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 			 struct audioformat *fmt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	struct usb_device *dev = chip->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	unsigned int ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	unsigned char data[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	if (get_iface_desc(alts)->bNumEndpoints < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	ep = get_endpoint(alts, 0)->bEndpointAddress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	data[0] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 			      USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 			      UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 			      data, sizeof(data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 		usb_audio_err(chip, "%d:%d: cannot set enable PITCH\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 			      iface, ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) static int init_pitch_v2(struct snd_usb_audio *chip, int iface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 			 struct usb_host_interface *alts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 			 struct audioformat *fmt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	struct usb_device *dev = chip->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	unsigned char data[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	data[0] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 			      USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 			      UAC2_EP_CS_PITCH << 8, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 			      data, sizeof(data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		usb_audio_err(chip, "%d:%d: cannot set enable PITCH (v2)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 			      iface, fmt->altsetting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258)  * initialize the pitch control and sample rate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 		       struct usb_host_interface *alts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 		       struct audioformat *fmt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	/* if endpoint doesn't have pitch control, bail out */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	switch (fmt->protocol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	case UAC_VERSION_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		return init_pitch_v1(chip, iface, alts, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	case UAC_VERSION_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 		return init_pitch_v2(chip, iface, alts, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) static int start_endpoints(struct snd_usb_substream *subs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	if (!subs->data_endpoint)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		struct snd_usb_endpoint *ep = subs->data_endpoint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 		dev_dbg(&subs->dev->dev, "Starting data EP @%p\n", ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 		ep->data_subs = subs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 		err = snd_usb_endpoint_start(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 			clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	if (subs->sync_endpoint &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	    !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 		struct snd_usb_endpoint *ep = subs->sync_endpoint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 		if (subs->data_endpoint->iface != subs->sync_endpoint->iface ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		    subs->data_endpoint->altsetting != subs->sync_endpoint->altsetting) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 			err = usb_set_interface(subs->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 						subs->sync_endpoint->iface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 						subs->sync_endpoint->altsetting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 			if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 				clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 				dev_err(&subs->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 					   "%d:%d: cannot set interface (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 					   subs->sync_endpoint->iface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 					   subs->sync_endpoint->altsetting, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 				return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		dev_dbg(&subs->dev->dev, "Starting sync EP @%p\n", ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		ep->sync_slave = subs->data_endpoint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		err = snd_usb_endpoint_start(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 			clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) static void sync_pending_stops(struct snd_usb_substream *subs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	snd_usb_endpoint_sync_pending_stop(subs->data_endpoint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) static void stop_endpoints(struct snd_usb_substream *subs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 		snd_usb_endpoint_stop(subs->sync_endpoint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 		snd_usb_endpoint_stop(subs->data_endpoint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) /* PCM sync_stop callback */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) static int snd_usb_pcm_sync_stop(struct snd_pcm_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	struct snd_usb_substream *subs = substream->runtime->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	sync_pending_stops(subs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) static int search_roland_implicit_fb(struct usb_device *dev, int ifnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 				     unsigned int altsetting,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 				     struct usb_host_interface **alts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 				     unsigned int *ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	struct usb_interface *iface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	struct usb_interface_descriptor *altsd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	struct usb_endpoint_descriptor *epd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	iface = usb_ifnum_to_if(dev, ifnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	if (!iface || iface->num_altsetting < altsetting + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	*alts = &iface->altsetting[altsetting];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	altsd = get_iface_desc(*alts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	if (altsd->bAlternateSetting != altsetting ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	    altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	    (altsd->bInterfaceSubClass != 2 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	     altsd->bInterfaceProtocol != 2   ) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	    altsd->bNumEndpoints < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	epd = get_endpoint(*alts, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	if (!usb_endpoint_is_isoc_in(epd) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	    (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 					USB_ENDPOINT_USAGE_IMPLICIT_FB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	*ep = epd->bEndpointAddress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) /* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384)  * applies. Returns 1 if a quirk was found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) static int set_sync_ep_implicit_fb_quirk(struct snd_usb_substream *subs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 					 struct usb_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 					 struct usb_interface_descriptor *altsd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 					 unsigned int attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	struct usb_host_interface *alts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	struct usb_interface *iface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	unsigned int ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	unsigned int ifnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	/* Implicit feedback sync EPs consumers are always playback EPs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	if (subs->direction != SNDRV_PCM_STREAM_PLAYBACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	switch (subs->stream->chip->usb_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	case USB_ID(0x22f0, 0x0006): /* Allen&Heath Qu-16 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 		ep = 0x81;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 		ifnum = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 		goto add_sync_ep_from_ifnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	case USB_ID(0x0763, 0x2081):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		ep = 0x81;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 		ifnum = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		goto add_sync_ep_from_ifnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	case USB_ID(0x2466, 0x8003): /* Fractal Audio Axe-Fx II */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	case USB_ID(0x0499, 0x172a): /* Yamaha MODX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		ep = 0x86;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 		ifnum = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		goto add_sync_ep_from_ifnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	case USB_ID(0x2466, 0x8010): /* Fractal Audio Axe-Fx III */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 		ep = 0x81;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 		ifnum = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 		goto add_sync_ep_from_ifnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	case USB_ID(0x1686, 0xf029): /* Zoom UAC-2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 		ep = 0x82;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 		ifnum = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		goto add_sync_ep_from_ifnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	case USB_ID(0x1397, 0x0001): /* Behringer UFX1604 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	case USB_ID(0x1397, 0x0002): /* Behringer UFX1204 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 		ep = 0x81;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		ifnum = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 		goto add_sync_ep_from_ifnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	case USB_ID(0x07fd, 0x0004): /* MOTU MicroBook II/IIc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 		/* MicroBook IIc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		if (altsd->bInterfaceClass == USB_CLASS_AUDIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 		/* MicroBook II */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		ep = 0x84;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 		ifnum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		goto add_sync_ep_from_ifnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	case USB_ID(0x07fd, 0x0008): /* MOTU M Series */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	case USB_ID(0x31e9, 0x0001): /* Solid State Logic SSL2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	case USB_ID(0x31e9, 0x0002): /* Solid State Logic SSL2+ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	case USB_ID(0x0499, 0x172f): /* Steinberg UR22C */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	case USB_ID(0x0d9a, 0x00df): /* RTX6001 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 		ep = 0x81;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 		ifnum = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 		goto add_sync_ep_from_ifnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	case USB_ID(0x2b73, 0x000a): /* Pioneer DJ DJM-900NXS2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	case USB_ID(0x2b73, 0x0017): /* Pioneer DJ DJM-250MK2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		ep = 0x82;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 		ifnum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 		goto add_sync_ep_from_ifnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	case USB_ID(0x0582, 0x01d8): /* BOSS Katana */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		/* BOSS Katana amplifiers do not need quirks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	if (attr == USB_ENDPOINT_SYNC_ASYNC &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	    altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	    altsd->bInterfaceProtocol == 2 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	    altsd->bNumEndpoints == 1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	    USB_ID_VENDOR(subs->stream->chip->usb_id) == 0x0582 /* Roland */ &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	    search_roland_implicit_fb(dev, altsd->bInterfaceNumber + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 				      altsd->bAlternateSetting,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 				      &alts, &ep) >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 		goto add_sync_ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	/* No quirk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) add_sync_ep_from_ifnum:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	iface = usb_ifnum_to_if(dev, ifnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	if (!iface || iface->num_altsetting < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	alts = &iface->altsetting[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) add_sync_ep:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 						   alts, ep, !subs->direction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 						   SND_USB_ENDPOINT_TYPE_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	if (!subs->sync_endpoint)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	subs->sync_endpoint->is_implicit_feedback = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	subs->data_endpoint->sync_master = subs->sync_endpoint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) static int set_sync_endpoint(struct snd_usb_substream *subs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 			     struct audioformat *fmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 			     struct usb_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 			     struct usb_host_interface *alts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 			     struct usb_interface_descriptor *altsd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	unsigned int ep, attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	bool implicit_fb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	/* we need a sync pipe in async OUT or adaptive IN mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	/* check the number of EP, since some devices have broken
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	 * descriptors which fool us.  if it has only one EP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	 * assume it as adaptive-out or sync-in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	if ((is_playback && (attr != USB_ENDPOINT_SYNC_ASYNC)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		(!is_playback && (attr != USB_ENDPOINT_SYNC_ADAPTIVE))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		 * In these modes the notion of sync_endpoint is irrelevant.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		 * Reset pointers to avoid using stale data from previously
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		 * used settings, e.g. when configuration and endpoints were
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 		 * changed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 		subs->sync_endpoint = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 		subs->data_endpoint->sync_master = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	err = set_sync_ep_implicit_fb_quirk(subs, dev, altsd, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	/* endpoint set by quirk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	if (err > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	if (altsd->bNumEndpoints < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	if ((is_playback && (attr == USB_ENDPOINT_SYNC_SYNC ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 			     attr == USB_ENDPOINT_SYNC_ADAPTIVE)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	    (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	 * In case of illegal SYNC_NONE for OUT endpoint, we keep going to see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	 * if we don't find a sync endpoint, as on M-Audio Transit. In case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	 * error fall back to SYNC mode and don't create sync endpoint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	/* check sync-pipe endpoint */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	/* ... and check descriptor size before accessing bSynchAddress
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	   because there is a version of the SB Audigy 2 NX firmware lacking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	   the audio fields in the endpoint descriptors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	    (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	     get_endpoint(alts, 1)->bSynchAddress != 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 		dev_err(&dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 			"%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 			   fmt->iface, fmt->altsetting,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 			   get_endpoint(alts, 1)->bmAttributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 			   get_endpoint(alts, 1)->bLength,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 			   get_endpoint(alts, 1)->bSynchAddress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	ep = get_endpoint(alts, 1)->bEndpointAddress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	    get_endpoint(alts, 0)->bSynchAddress != 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	    ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	     (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		dev_err(&dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 			"%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 			   fmt->iface, fmt->altsetting,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 			   is_playback, ep, get_endpoint(alts, 0)->bSynchAddress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 	implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 			== USB_ENDPOINT_USAGE_IMPLICIT_FB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 						   alts, ep, !subs->direction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 						   implicit_fb ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 							SND_USB_ENDPOINT_TYPE_DATA :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 							SND_USB_ENDPOINT_TYPE_SYNC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	if (!subs->sync_endpoint) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		if (is_playback && attr == USB_ENDPOINT_SYNC_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	subs->sync_endpoint->is_implicit_feedback = implicit_fb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	subs->data_endpoint->sync_master = subs->sync_endpoint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601)  * find a matching format and set up the interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	struct usb_device *dev = subs->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	struct usb_host_interface *alts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	struct usb_interface_descriptor *altsd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	struct usb_interface *iface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	iface = usb_ifnum_to_if(dev, fmt->iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	if (WARN_ON(!iface))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	alts = usb_altnum_to_altsetting(iface, fmt->altsetting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	if (WARN_ON(!alts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	altsd = get_iface_desc(alts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	if (fmt == subs->cur_audiofmt && !subs->need_setup_fmt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	/* close the old interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	if (subs->interface >= 0 && (subs->interface != fmt->iface || subs->need_setup_fmt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		if (!subs->stream->chip->keep_iface) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 			err = usb_set_interface(subs->dev, subs->interface, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 			if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 				dev_err(&dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 					"%d:%d: return to setting 0 failed (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 					fmt->iface, fmt->altsetting, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 				return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 		subs->interface = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 		subs->altset_idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	if (subs->need_setup_fmt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		subs->need_setup_fmt = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	/* set interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	if (iface->cur_altsetting != alts) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		err = snd_usb_select_mode_quirk(subs, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		err = usb_set_interface(dev, fmt->iface, fmt->altsetting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 			dev_err(&dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 				"%d:%d: usb_set_interface failed (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 				fmt->iface, fmt->altsetting, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 		dev_dbg(&dev->dev, "setting usb interface %d:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 			fmt->iface, fmt->altsetting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 		err = snd_vendor_set_pcm_intf(iface, fmt->iface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 					      fmt->altsetting, subs->direction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 		snd_usb_set_interface_quirk(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	subs->interface = fmt->iface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	subs->altset_idx = fmt->altset_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 						   alts, fmt->endpoint, subs->direction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 						   SND_USB_ENDPOINT_TYPE_DATA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	if (!subs->data_endpoint)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	err = set_sync_endpoint(subs, fmt, dev, alts, altsd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	subs->cur_audiofmt = fmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	snd_usb_set_format_quirk(subs, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) static int snd_usb_pcm_change_state(struct snd_usb_substream *subs, int state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) int snd_usb_enable_audio_stream(struct snd_usb_substream *subs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	int datainterval, bool enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	struct audioformat *fmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	struct usb_host_interface *alts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	struct usb_interface *iface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	if (!enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 		if (subs->interface >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 			usb_set_interface(subs->dev, subs->interface, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 			subs->altset_idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 			subs->interface = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 			subs->cur_audiofmt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		snd_usb_autosuspend(subs->stream->chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	snd_usb_autoresume(subs->stream->chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	if (datainterval != -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 		fmt = find_format_and_si(subs, datainterval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 		fmt = find_format(subs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	if (!fmt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		dev_err(&subs->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		"cannot set format: format = %#x, rate = %d, channels = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 			   subs->pcm_format, subs->cur_rate, subs->channels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	subs->altset_idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	subs->interface = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	if (atomic_read(&subs->stream->chip->shutdown)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		ret = set_format(subs, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		if (!iface) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 			dev_err(&subs->dev->dev, "Could not get iface %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 				subs->cur_audiofmt->iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		ret = snd_usb_init_sample_rate(subs->stream->chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 					       subs->cur_audiofmt->iface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 					       alts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 					       subs->cur_audiofmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 					       subs->cur_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 			dev_err(&subs->dev->dev, "failed to set rate %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 				subs->cur_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	subs->interface = fmt->iface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	subs->altset_idx = fmt->altset_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) EXPORT_SYMBOL_GPL(snd_usb_enable_audio_stream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762)  * Return the score of matching two audioformats.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763)  * Veto the audioformat if:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764)  * - It has no channels for some reason.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765)  * - Requested PCM format is not supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766)  * - Requested sample rate is not supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) static int match_endpoint_audioformats(struct snd_usb_substream *subs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 				       struct audioformat *fp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 				       struct audioformat *match, int rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 				       snd_pcm_format_t pcm_format)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	int score = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	if (fp->channels < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 		dev_dbg(&subs->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 			"%s: (fmt @%p) no channels\n", __func__, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	if (!(fp->formats & pcm_format_to_bits(pcm_format))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		dev_dbg(&subs->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 			"%s: (fmt @%p) no match for format %d\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 			fp, pcm_format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	for (i = 0; i < fp->nr_rates; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		if (fp->rate_table[i] == rate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 			score++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	if (!score) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 		dev_dbg(&subs->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 			"%s: (fmt @%p) no match for rate %d\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 			fp, rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	if (fp->channels == match->channels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 		score++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	dev_dbg(&subs->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		"%s: (fmt @%p) score %d\n", __func__, fp, score);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	return score;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812)  * Configure the sync ep using the rate and pcm format of the data ep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) static int configure_sync_endpoint(struct snd_usb_substream *subs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	struct audioformat *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	struct audioformat *sync_fp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	int cur_score = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	int sync_period_bytes = subs->period_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	struct snd_usb_substream *sync_subs =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		&subs->stream->substream[subs->direction ^ 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	    !subs->stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		return snd_usb_endpoint_set_params(subs->sync_endpoint,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 						   subs->pcm_format,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 						   subs->channels,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 						   subs->period_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 						   0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 						   subs->cur_rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 						   subs->cur_audiofmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 						   NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	/* Try to find the best matching audioformat. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	list_for_each_entry(fp, &sync_subs->fmt_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		int score = match_endpoint_audioformats(subs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 							fp, subs->cur_audiofmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 			subs->cur_rate, subs->pcm_format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		if (score > cur_score) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 			sync_fp = fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 			cur_score = score;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	if (unlikely(sync_fp == NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		dev_err(&subs->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 			"%s: no valid audioformat for sync ep %x found\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 			__func__, sync_subs->ep_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	 * Recalculate the period bytes if channel number differ between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	 * data and sync ep audioformat.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	if (sync_fp->channels != subs->channels) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		sync_period_bytes = (subs->period_bytes / subs->channels) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 			sync_fp->channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		dev_dbg(&subs->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 			"%s: adjusted sync ep period bytes (%d -> %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 			__func__, subs->period_bytes, sync_period_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	ret = snd_usb_endpoint_set_params(subs->sync_endpoint,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 					  subs->pcm_format,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 					  sync_fp->channels,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 					  sync_period_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 					  0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 					  subs->cur_rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 					  sync_fp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 					  NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879)  * configure endpoint params
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881)  * called  during initial setup and upon resume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) static int configure_endpoint(struct snd_usb_substream *subs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	/* format changed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	stop_endpoints(subs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	sync_pending_stops(subs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	ret = snd_usb_endpoint_set_params(subs->data_endpoint,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 					  subs->pcm_format,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 					  subs->channels,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 					  subs->period_bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 					  subs->period_frames,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 					  subs->buffer_periods,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 					  subs->cur_rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 					  subs->cur_audiofmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 					  subs->sync_endpoint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	if (subs->sync_endpoint)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 		ret = configure_sync_endpoint(subs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) static int snd_usb_pcm_change_state(struct snd_usb_substream *subs, int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	if (!subs->str_pd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	ret = snd_usb_power_domain_set(subs->stream->chip, subs->str_pd, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		dev_err(&subs->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 			"Cannot change Power Domain ID: %d to state: %d. Err: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 			subs->str_pd->pd_id, state, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) int snd_usb_pcm_suspend(struct snd_usb_stream *as)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) int snd_usb_pcm_resume(struct snd_usb_stream *as)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	ret = snd_usb_pcm_change_state(&as->substream[0], UAC3_PD_STATE_D1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	ret = snd_usb_pcm_change_state(&as->substream[1], UAC3_PD_STATE_D1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957)  * hw_params callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959)  * allocate a buffer and set the given audio format.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961)  * so far we use a physically linear buffer although packetize transfer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962)  * doesn't need a continuous area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963)  * if sg buffer is supported on the later version of alsa, we'll follow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964)  * that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) static int snd_usb_hw_params(struct snd_pcm_substream *substream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 			     struct snd_pcm_hw_params *hw_params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	struct snd_usb_substream *subs = substream->runtime->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	struct audioformat *fmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	ret = snd_media_start_pipeline(subs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	subs->pcm_format = params_format(hw_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	subs->period_bytes = params_period_bytes(hw_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	subs->period_frames = params_period_size(hw_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	subs->buffer_periods = params_periods(hw_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	subs->channels = params_channels(hw_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	subs->cur_rate = params_rate(hw_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	fmt = find_format(subs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	if (!fmt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 		dev_dbg(&subs->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 			"cannot set format: format = %#x, rate = %d, channels = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 			   subs->pcm_format, subs->cur_rate, subs->channels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		goto stop_pipeline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	ret = snd_usb_lock_shutdown(subs->stream->chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		goto stop_pipeline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	ret = set_format(subs, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	subs->interface = fmt->iface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	subs->altset_idx = fmt->altset_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	subs->need_setup_ep = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009)  unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	snd_usb_unlock_shutdown(subs->stream->chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		goto stop_pipeline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015)  stop_pipeline:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	snd_media_stop_pipeline(subs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021)  * hw_free callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023)  * reset the audio format and release the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) static int snd_usb_hw_free(struct snd_pcm_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	struct snd_usb_substream *subs = substream->runtime->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	snd_media_stop_pipeline(subs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	subs->cur_audiofmt = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	subs->cur_rate = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	subs->period_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	if (!snd_usb_lock_shutdown(subs->stream->chip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 		stop_endpoints(subs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 		sync_pending_stops(subs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 		snd_usb_endpoint_deactivate(subs->sync_endpoint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 		snd_usb_endpoint_deactivate(subs->data_endpoint);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 		snd_usb_unlock_shutdown(subs->stream->chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045)  * prepare callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047)  * only a few subtle things...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	struct snd_pcm_runtime *runtime = substream->runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	struct snd_usb_substream *subs = runtime->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	struct usb_host_interface *alts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	struct usb_interface *iface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	ret = snd_vendor_set_pcm_buf(subs->dev, subs->cur_audiofmt->iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	if (! subs->cur_audiofmt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		dev_err(&subs->dev->dev, "no format is specified!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	ret = snd_usb_lock_shutdown(subs->stream->chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	if (snd_BUG_ON(!subs->data_endpoint)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 		ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	ret = set_format(subs, subs->cur_audiofmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	if (subs->need_setup_ep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		alts = &iface->altsetting[subs->cur_audiofmt->altset_idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 		ret = snd_usb_init_sample_rate(subs->stream->chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 					       subs->cur_audiofmt->iface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 					       alts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 					       subs->cur_audiofmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 					       subs->cur_rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 			goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 		if (snd_vendor_get_ops()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 			ret = snd_vendor_set_rate(iface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 						  subs->cur_audiofmt->iface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 						  subs->cur_rate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 						  subs->cur_audiofmt->altsetting);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 			if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 				subs->need_setup_ep = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 				goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		ret = configure_endpoint(subs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 			goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 		subs->need_setup_ep = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 	/* some unit conversions in runtime */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	subs->data_endpoint->maxframesize =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 		bytes_to_frames(runtime, subs->data_endpoint->maxpacksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	subs->data_endpoint->curframesize =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 		bytes_to_frames(runtime, subs->data_endpoint->curpacksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	/* reset the pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 	subs->hwptr_done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	subs->transfer_done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	subs->last_delay = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	subs->last_frame_number = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	runtime->delay = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	/* for playback, submit the URBs now; otherwise, the first hwptr_done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	 * updates for all URBs would happen at the same time when starting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 		ret = start_endpoints(subs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129)  unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	snd_usb_unlock_shutdown(subs->stream->chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) static const struct snd_pcm_hardware snd_usb_hardware =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	.info =			SNDRV_PCM_INFO_MMAP |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 				SNDRV_PCM_INFO_MMAP_VALID |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 				SNDRV_PCM_INFO_BATCH |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 				SNDRV_PCM_INFO_INTERLEAVED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 				SNDRV_PCM_INFO_BLOCK_TRANSFER |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 				SNDRV_PCM_INFO_PAUSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	.buffer_bytes_max =	1024 * 1024,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	.period_bytes_min =	64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	.period_bytes_max =	512 * 1024,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	.periods_min =		2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	.periods_max =		1024,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) static int hw_check_valid_format(struct snd_usb_substream *subs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 				 struct snd_pcm_hw_params *params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 				 struct audioformat *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	struct snd_mask check_fmts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	unsigned int ptime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	/* check the format */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	snd_mask_none(&check_fmts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	check_fmts.bits[0] = (u32)fp->formats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	check_fmts.bits[1] = (u32)(fp->formats >> 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	snd_mask_intersect(&check_fmts, fmts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	if (snd_mask_empty(&check_fmts)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 		hwc_debug("   > check: no supported format %d\n", fp->format);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	/* check the channels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	if (fp->channels < ct->min || fp->channels > ct->max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 		hwc_debug("   > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	/* check the rate is within the range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		hwc_debug("   > check: rate_min %d > max %d\n", fp->rate_min, it->max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 		hwc_debug("   > check: rate_max %d < min %d\n", fp->rate_max, it->min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	/* check whether the period time is >= the data packet interval */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 	if (subs->speed != USB_SPEED_FULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 		ptime = 125 * (1 << fp->datainterval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		if (ptime > pt->max || (ptime == pt->max && pt->openmax)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 			hwc_debug("   > check: ptime %u > max %u\n", ptime, pt->max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) static int hw_rule_rate(struct snd_pcm_hw_params *params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 			struct snd_pcm_hw_rule *rule)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	struct snd_usb_substream *subs = rule->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	struct audioformat *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	unsigned int rmin, rmax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	int changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	changed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	rmin = rmax = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 	list_for_each_entry(fp, &subs->fmt_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 		if (!hw_check_valid_format(subs, params, fp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		if (changed++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 			if (rmin > fp->rate_min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 				rmin = fp->rate_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 			if (rmax < fp->rate_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 				rmax = fp->rate_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 			rmin = fp->rate_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 			rmax = fp->rate_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	if (!changed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		hwc_debug("  --> get empty\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 		it->empty = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	changed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	if (it->min < rmin) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 		it->min = rmin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 		it->openmin = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 		changed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	if (it->max > rmax) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 		it->max = rmax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 		it->openmax = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		changed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	if (snd_interval_checkempty(it)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 		it->empty = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	hwc_debug("  --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	return changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) static int hw_rule_channels(struct snd_pcm_hw_params *params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 			    struct snd_pcm_hw_rule *rule)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	struct snd_usb_substream *subs = rule->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	struct audioformat *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	unsigned int rmin, rmax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	int changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	changed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	rmin = rmax = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	list_for_each_entry(fp, &subs->fmt_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		if (!hw_check_valid_format(subs, params, fp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 		if (changed++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 			if (rmin > fp->channels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 				rmin = fp->channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 			if (rmax < fp->channels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 				rmax = fp->channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 			rmin = fp->channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 			rmax = fp->channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	if (!changed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 		hwc_debug("  --> get empty\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 		it->empty = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	changed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	if (it->min < rmin) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 		it->min = rmin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		it->openmin = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 		changed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	if (it->max > rmax) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 		it->max = rmax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 		it->openmax = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 		changed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 	if (snd_interval_checkempty(it)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 		it->empty = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	hwc_debug("  --> (%d, %d) (changed = %d)\n", it->min, it->max, changed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	return changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) static int hw_rule_format(struct snd_pcm_hw_params *params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 			  struct snd_pcm_hw_rule *rule)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	struct snd_usb_substream *subs = rule->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	struct audioformat *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	u64 fbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	u32 oldbits[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	int changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	fbits = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	list_for_each_entry(fp, &subs->fmt_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 		if (!hw_check_valid_format(subs, params, fp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 		fbits |= fp->formats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	oldbits[0] = fmt->bits[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	oldbits[1] = fmt->bits[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	fmt->bits[0] &= (u32)fbits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	fmt->bits[1] &= (u32)(fbits >> 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	if (!fmt->bits[0] && !fmt->bits[1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 		hwc_debug("  --> get empty\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	hwc_debug("  --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	return changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) static int hw_rule_period_time(struct snd_pcm_hw_params *params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 			       struct snd_pcm_hw_rule *rule)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	struct snd_usb_substream *subs = rule->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	struct audioformat *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	struct snd_interval *it;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	unsigned char min_datainterval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	unsigned int pmin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	int changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	min_datainterval = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	list_for_each_entry(fp, &subs->fmt_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 		if (!hw_check_valid_format(subs, params, fp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 		min_datainterval = min(min_datainterval, fp->datainterval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 	if (min_datainterval == 0xff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 		hwc_debug("  --> get empty\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 		it->empty = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	pmin = 125 * (1 << min_datainterval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	changed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 	if (it->min < pmin) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 		it->min = pmin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		it->openmin = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 		changed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 	if (snd_interval_checkempty(it)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 		it->empty = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	hwc_debug("  --> (%u,%u) (changed = %d)\n", it->min, it->max, changed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	return changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367)  *  If the device supports unusual bit rates, does the request meet these?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 				  struct snd_usb_substream *subs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	struct audioformat *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	int *rate_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	int count = 0, needs_knot = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	kfree(subs->rate_list.list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	subs->rate_list.list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	list_for_each_entry(fp, &subs->fmt_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 		if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 		count += fp->nr_rates;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 		if (fp->rates & SNDRV_PCM_RATE_KNOT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 			needs_knot = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	if (!needs_knot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	subs->rate_list.list = rate_list =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 		kmalloc_array(count, sizeof(int), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	if (!subs->rate_list.list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	subs->rate_list.count = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	subs->rate_list.mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	list_for_each_entry(fp, &subs->fmt_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 		for (i = 0; i < fp->nr_rates; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 			rate_list[count++] = fp->rate_table[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 					 &subs->rate_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412)  * set up the runtime hardware information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 	struct audioformat *fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	unsigned int pt, ptmin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	int param_period_time_if_needed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	runtime->hw.formats = subs->formats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 	runtime->hw.rate_min = 0x7fffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 	runtime->hw.rate_max = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	runtime->hw.channels_min = 256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	runtime->hw.channels_max = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	runtime->hw.rates = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	ptmin = UINT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	/* check min/max rates and channels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 	list_for_each_entry(fp, &subs->fmt_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		runtime->hw.rates |= fp->rates;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 		if (runtime->hw.rate_min > fp->rate_min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 			runtime->hw.rate_min = fp->rate_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 		if (runtime->hw.rate_max < fp->rate_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 			runtime->hw.rate_max = fp->rate_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 		if (runtime->hw.channels_min > fp->channels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 			runtime->hw.channels_min = fp->channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 		if (runtime->hw.channels_max < fp->channels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 			runtime->hw.channels_max = fp->channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 		if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 			/* FIXME: there might be more than one audio formats... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 			runtime->hw.period_bytes_min = runtime->hw.period_bytes_max =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 				fp->frame_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 		pt = 125 * (1 << fp->datainterval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 		ptmin = min(ptmin, pt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 	param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	if (subs->speed == USB_SPEED_FULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 		/* full speed devices have fixed data packet interval */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 		ptmin = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	if (ptmin == 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 		/* if period time doesn't go below 1 ms, no rules needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 		param_period_time_if_needed = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	err = snd_pcm_hw_constraint_minmax(runtime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 					   SNDRV_PCM_HW_PARAM_PERIOD_TIME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 					   ptmin, UINT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 				  hw_rule_rate, subs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 				  SNDRV_PCM_HW_PARAM_FORMAT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 				  SNDRV_PCM_HW_PARAM_CHANNELS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 				  param_period_time_if_needed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 				  -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 				  hw_rule_channels, subs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 				  SNDRV_PCM_HW_PARAM_FORMAT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 				  SNDRV_PCM_HW_PARAM_RATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 				  param_period_time_if_needed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 				  -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 				  hw_rule_format, subs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 				  SNDRV_PCM_HW_PARAM_RATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 				  SNDRV_PCM_HW_PARAM_CHANNELS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 				  param_period_time_if_needed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 				  -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	if (param_period_time_if_needed >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 		err = snd_pcm_hw_rule_add(runtime, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 					  SNDRV_PCM_HW_PARAM_PERIOD_TIME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 					  hw_rule_period_time, subs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 					  SNDRV_PCM_HW_PARAM_FORMAT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 					  SNDRV_PCM_HW_PARAM_CHANNELS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 					  SNDRV_PCM_HW_PARAM_RATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 					  -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	err = snd_usb_pcm_check_knot(runtime, subs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	return snd_usb_autoresume(subs->stream->chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) static int snd_usb_pcm_open(struct snd_pcm_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	int direction = substream->stream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 	struct snd_pcm_runtime *runtime = substream->runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	struct snd_usb_substream *subs = &as->substream[direction];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	bool is_support = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	ret = snd_vendor_set_pcm_connection(subs->dev, SOUND_PCM_OPEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 					    direction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	subs->interface = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 	subs->altset_idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	runtime->hw = snd_usb_hardware;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	runtime->private_data = subs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	subs->pcm_substream = substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	/* runtime PM is also done there */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	/* initialize DSD/DOP context */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	subs->dsd_dop.byte_idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	subs->dsd_dop.channel = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	subs->dsd_dop.marker = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	ret = setup_hw_info(runtime, subs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 	if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 		ret = snd_media_stream_init(subs, as->pcm, direction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 			snd_usb_autosuspend(subs->stream->chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	trace_android_vh_sound_usb_support_cpu_suspend(subs->dev, direction, &is_support);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 	if (!ret && is_support)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 		snd_usb_autosuspend(subs->stream->chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) static int snd_usb_pcm_close(struct snd_pcm_substream *substream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	int direction = substream->stream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	struct snd_usb_stream *as = snd_pcm_substream_chip(substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	struct snd_usb_substream *subs = &as->substream[direction];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	bool is_support = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	ret = snd_vendor_set_pcm_connection(subs->dev, SOUND_PCM_CLOSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 					    direction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	trace_android_vh_sound_usb_support_cpu_suspend(subs->dev, direction, &is_support);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	if (!ret && is_support)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 		snd_usb_autoresume(subs->stream->chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 	snd_media_stop_pipeline(subs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	if (!as->chip->keep_iface &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	    subs->interface >= 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	    !snd_usb_lock_shutdown(subs->stream->chip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 		usb_set_interface(subs->dev, subs->interface, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 		ret = snd_vendor_set_pcm_intf(usb_ifnum_to_if(subs->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 							      subs->interface),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 					      subs->interface, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 					      direction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 		subs->interface = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 		ret = snd_usb_pcm_change_state(subs, UAC3_PD_STATE_D1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 		snd_usb_unlock_shutdown(subs->stream->chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 	subs->pcm_substream = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 	snd_usb_autosuspend(subs->stream->chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) /* Since a URB can handle only a single linear buffer, we must use double
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589)  * buffering when the data to be transferred overflows the buffer boundary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590)  * To avoid inconsistencies when updating hwptr_done, we use double buffering
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591)  * for all URBs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) static void retire_capture_urb(struct snd_usb_substream *subs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 			       struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	unsigned int stride, frames, bytes, oldptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 	int i, period_elapsed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 	unsigned char *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 	int current_frame_number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 	/* read frame number here, update pointer in critical section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 	current_frame_number = usb_get_current_frame_number(subs->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 	stride = runtime->frame_bits >> 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 	for (i = 0; i < urb->number_of_packets; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 		cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 		if (urb->iso_frame_desc[i].status && printk_ratelimit()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 			dev_dbg(&subs->dev->dev, "frame %d active: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 				i, urb->iso_frame_desc[i].status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 			// continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 		bytes = urb->iso_frame_desc[i].actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 		if (subs->stream_offset_adj > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 			unsigned int adj = min(subs->stream_offset_adj, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 			cp += adj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 			bytes -= adj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 			subs->stream_offset_adj -= adj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 		frames = bytes / stride;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 		if (!subs->txfr_quirk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 			bytes = frames * stride;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 		if (bytes % (runtime->sample_bits >> 3) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 			int oldbytes = bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 			bytes = frames * stride;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 			dev_warn_ratelimited(&subs->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 				 "Corrected urb data len. %d->%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 							oldbytes, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 		/* update the current pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 		spin_lock_irqsave(&subs->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 		oldptr = subs->hwptr_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 		subs->hwptr_done += bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 		if (subs->hwptr_done >= runtime->buffer_size * stride)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 			subs->hwptr_done -= runtime->buffer_size * stride;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 		frames = (bytes + (oldptr % stride)) / stride;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 		subs->transfer_done += frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 		if (subs->transfer_done >= runtime->period_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 			subs->transfer_done -= runtime->period_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 			period_elapsed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 		/* capture delay is by construction limited to one URB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 		 * reset delays here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 		runtime->delay = subs->last_delay = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 		/* realign last_frame_number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 		subs->last_frame_number = current_frame_number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 		subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 		spin_unlock_irqrestore(&subs->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 		/* copy a data chunk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 		if (oldptr + bytes > runtime->buffer_size * stride) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 			unsigned int bytes1 =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 					runtime->buffer_size * stride - oldptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 			memcpy(runtime->dma_area + oldptr, cp, bytes1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 			memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 			memcpy(runtime->dma_area + oldptr, cp, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 	if (period_elapsed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 		snd_pcm_period_elapsed(subs->pcm_substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 					     struct urb *urb, unsigned int bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 	unsigned int stride = runtime->frame_bits >> 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 	unsigned int dst_idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 	unsigned int src_idx = subs->hwptr_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 	unsigned int wrap = runtime->buffer_size * stride;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 	u8 *dst = urb->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 	u8 *src = runtime->dma_area;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 	u8 marker[] = { 0x05, 0xfa };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 	 * The DSP DOP format defines a way to transport DSD samples over
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 	 * normal PCM data endpoints. It requires stuffing of marker bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	 * (0x05 and 0xfa, alternating per sample frame), and then expects
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	 * 2 additional bytes of actual payload. The whole frame is stored
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 	 * LSB.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 	 * Hence, for a stereo transport, the buffer layout looks like this,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	 * where L refers to left channel samples and R to right.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 	 *   L1 L2 0x05   R1 R2 0x05   L3 L4 0xfa  R3 R4 0xfa
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	 *   L5 L6 0x05   R5 R6 0x05   L7 L8 0xfa  R7 R8 0xfa
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 	 *   .....
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 	while (bytes--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 		if (++subs->dsd_dop.byte_idx == 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 			/* frame boundary? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 			dst[dst_idx++] = marker[subs->dsd_dop.marker];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 			src_idx += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 			subs->dsd_dop.byte_idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 			if (++subs->dsd_dop.channel % runtime->channels == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 				/* alternate the marker */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 				subs->dsd_dop.marker++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 				subs->dsd_dop.marker %= ARRAY_SIZE(marker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 				subs->dsd_dop.channel = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 			/* stuff the DSD payload */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 			int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 			if (subs->cur_audiofmt->dsd_bitrev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 				dst[dst_idx++] = bitrev8(src[idx]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 				dst[dst_idx++] = src[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 			subs->hwptr_done++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 	if (subs->hwptr_done >= runtime->buffer_size * stride)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 		subs->hwptr_done -= runtime->buffer_size * stride;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) static void copy_to_urb(struct snd_usb_substream *subs, struct urb *urb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 			int offset, int stride, unsigned int bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	if (subs->hwptr_done + bytes > runtime->buffer_size * stride) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 		/* err, the transferred area goes over buffer boundary. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 		unsigned int bytes1 =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 			runtime->buffer_size * stride - subs->hwptr_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 		memcpy(urb->transfer_buffer + offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 		       runtime->dma_area + subs->hwptr_done, bytes1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 		memcpy(urb->transfer_buffer + offset + bytes1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 		       runtime->dma_area, bytes - bytes1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 		memcpy(urb->transfer_buffer + offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 		       runtime->dma_area + subs->hwptr_done, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 	subs->hwptr_done += bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 	if (subs->hwptr_done >= runtime->buffer_size * stride)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 		subs->hwptr_done -= runtime->buffer_size * stride;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) static unsigned int copy_to_urb_quirk(struct snd_usb_substream *subs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 				      struct urb *urb, int stride,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 				      unsigned int bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	__le32 packet_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) 	/* Put __le32 length descriptor at start of each packet. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 	for (i = 0; i < urb->number_of_packets; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) 		unsigned int length = urb->iso_frame_desc[i].length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 		unsigned int offset = urb->iso_frame_desc[i].offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 		packet_length = cpu_to_le32(length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 		offset += i * sizeof(packet_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 		urb->iso_frame_desc[i].offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 		urb->iso_frame_desc[i].length += sizeof(packet_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 		memcpy(urb->transfer_buffer + offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 		       &packet_length, sizeof(packet_length));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 		copy_to_urb(subs, urb, offset + sizeof(packet_length),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 			    stride, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	/* Adjust transfer size accordingly. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	bytes += urb->number_of_packets * sizeof(packet_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 	return bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) static void prepare_playback_urb(struct snd_usb_substream *subs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 				 struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 	struct snd_usb_endpoint *ep = subs->data_endpoint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 	struct snd_urb_ctx *ctx = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 	unsigned int counts, frames, bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	int i, stride, period_elapsed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	stride = runtime->frame_bits >> 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	frames = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	urb->number_of_packets = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 	spin_lock_irqsave(&subs->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 	subs->frame_limit += ep->max_urb_frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 	for (i = 0; i < ctx->packets; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 		if (ctx->packet_size[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 			counts = ctx->packet_size[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 		else if (ep->sync_master)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 			counts = snd_usb_endpoint_slave_next_packet_size(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 			counts = snd_usb_endpoint_next_packet_size(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 		/* set up descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 		urb->iso_frame_desc[i].offset = frames * ep->stride;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 		urb->iso_frame_desc[i].length = counts * ep->stride;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 		frames += counts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 		urb->number_of_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 		subs->transfer_done += counts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 		if (subs->transfer_done >= runtime->period_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 			subs->transfer_done -= runtime->period_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 			subs->frame_limit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 			period_elapsed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 			if (subs->fmt_type == UAC_FORMAT_TYPE_II) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 				if (subs->transfer_done > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 					/* FIXME: fill-max mode is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 					 * supported yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 					frames -= subs->transfer_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 					counts -= subs->transfer_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 					urb->iso_frame_desc[i].length =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 						counts * ep->stride;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 					subs->transfer_done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 				i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 				if (i < ctx->packets) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 					/* add a transfer delimiter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 					urb->iso_frame_desc[i].offset =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 						frames * ep->stride;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 					urb->iso_frame_desc[i].length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 					urb->number_of_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 		/* finish at the period boundary or after enough frames */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 		if ((period_elapsed ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 				subs->transfer_done >= subs->frame_limit) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 		    !snd_usb_endpoint_implicit_feedback_sink(ep))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 	bytes = frames * ep->stride;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 	if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 		     subs->cur_audiofmt->dsd_dop)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 		fill_playback_urb_dsd_dop(subs, urb, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 	} else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 			   subs->cur_audiofmt->dsd_bitrev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 		/* bit-reverse the bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 		u8 *buf = urb->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 		for (i = 0; i < bytes; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 			int idx = (subs->hwptr_done + i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 				% (runtime->buffer_size * stride);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 			buf[i] = bitrev8(runtime->dma_area[idx]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 		subs->hwptr_done += bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 		if (subs->hwptr_done >= runtime->buffer_size * stride)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 			subs->hwptr_done -= runtime->buffer_size * stride;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 		/* usual PCM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 		if (!subs->tx_length_quirk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 			copy_to_urb(subs, urb, 0, stride, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 			bytes = copy_to_urb_quirk(subs, urb, stride, bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 			/* bytes is now amount of outgoing data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 	/* update delay with exact number of samples queued */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 	runtime->delay = subs->last_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 	runtime->delay += frames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 	subs->last_delay = runtime->delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 	/* realign last_frame_number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 	subs->last_frame_number = usb_get_current_frame_number(subs->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	subs->last_frame_number &= 0xFF; /* keep 8 LSBs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 	if (subs->trigger_tstamp_pending_update) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 		/* this is the first actual URB submitted,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 		 * update trigger timestamp to reflect actual start time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 		snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 		subs->trigger_tstamp_pending_update = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 	spin_unlock_irqrestore(&subs->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 	urb->transfer_buffer_length = bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 	if (period_elapsed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 		snd_pcm_period_elapsed(subs->pcm_substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886)  * process after playback data complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887)  * - decrease the delay count again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) static void retire_playback_urb(struct snd_usb_substream *subs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) 			       struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) 	struct snd_usb_endpoint *ep = subs->data_endpoint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	int processed = urb->transfer_buffer_length / ep->stride;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 	int est_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 	/* ignore the delay accounting when processed=0 is given, i.e.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	 * silent payloads are processed before handling the actual data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 	if (!processed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 	spin_lock_irqsave(&subs->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	if (!subs->last_delay)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 		goto out; /* short path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	est_delay = snd_usb_pcm_delay(subs, runtime->rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 	/* update delay with exact number of samples played */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 	if (processed > subs->last_delay)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 		subs->last_delay = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 		subs->last_delay -= processed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 	runtime->delay = subs->last_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 	 * Report when delay estimate is off by more than 2ms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	 * The error should be lower than 2ms since the estimate relies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 	 * on two reads of a counter updated every ms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 	if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 		dev_dbg_ratelimited(&subs->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 			"delay: estimated %d, actual %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 			est_delay, subs->last_delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 	if (!subs->running) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 		/* update last_frame_number for delay counting here since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 		 * prepare_playback_urb won't be called during pause
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 		subs->last_frame_number =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 			usb_get_current_frame_number(subs->dev) & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 	spin_unlock_irqrestore(&subs->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 					      int cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 	struct snd_usb_substream *subs = substream->runtime->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 	case SNDRV_PCM_TRIGGER_START:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 		subs->trigger_tstamp_pending_update = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 		subs->data_endpoint->prepare_data_urb = prepare_playback_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 		subs->data_endpoint->retire_data_urb = retire_playback_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 		subs->running = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 	case SNDRV_PCM_TRIGGER_STOP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 		stop_endpoints(subs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 		subs->running = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 		subs->data_endpoint->prepare_data_urb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 		/* keep retire_data_urb for delay calculation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 		subs->data_endpoint->retire_data_urb = retire_playback_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 		subs->running = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	case SNDRV_PCM_TRIGGER_SUSPEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 		if (subs->stream->chip->setup_fmt_after_resume_quirk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 			stop_endpoints(subs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 			subs->need_setup_fmt = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 					     int cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 	struct snd_usb_substream *subs = substream->runtime->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 	case SNDRV_PCM_TRIGGER_START:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 		err = start_endpoints(subs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 		subs->data_endpoint->retire_data_urb = retire_capture_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 		subs->running = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 	case SNDRV_PCM_TRIGGER_STOP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 		stop_endpoints(subs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 		subs->data_endpoint->retire_data_urb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 		subs->running = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 		subs->data_endpoint->retire_data_urb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 		subs->running = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 		subs->data_endpoint->retire_data_urb = retire_capture_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 		subs->running = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 	case SNDRV_PCM_TRIGGER_SUSPEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 		if (subs->stream->chip->setup_fmt_after_resume_quirk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 			stop_endpoints(subs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 			subs->need_setup_fmt = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) static const struct snd_pcm_ops snd_usb_playback_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 	.open =		snd_usb_pcm_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 	.close =	snd_usb_pcm_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 	.hw_params =	snd_usb_hw_params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 	.hw_free =	snd_usb_hw_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 	.prepare =	snd_usb_pcm_prepare,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 	.trigger =	snd_usb_substream_playback_trigger,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 	.sync_stop =	snd_usb_pcm_sync_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 	.pointer =	snd_usb_pcm_pointer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) static const struct snd_pcm_ops snd_usb_capture_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 	.open =		snd_usb_pcm_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 	.close =	snd_usb_pcm_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 	.hw_params =	snd_usb_hw_params,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 	.hw_free =	snd_usb_hw_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 	.prepare =	snd_usb_pcm_prepare,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 	.trigger =	snd_usb_substream_capture_trigger,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 	.sync_stop =	snd_usb_pcm_sync_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 	.pointer =	snd_usb_pcm_pointer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 	const struct snd_pcm_ops *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 	ops = stream == SNDRV_PCM_STREAM_PLAYBACK ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 			&snd_usb_playback_ops : &snd_usb_capture_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 	snd_pcm_set_ops(pcm, stream, ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) void snd_usb_preallocate_buffer(struct snd_usb_substream *subs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 	struct snd_pcm *pcm = subs->stream->pcm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 	struct snd_pcm_substream *s = pcm->streams[subs->direction].substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 	struct device *dev = subs->dev->bus->sysdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 	if (snd_usb_use_vmalloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 		snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_VMALLOC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 					   NULL, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 		snd_pcm_set_managed_buffer(s, SNDRV_DMA_TYPE_DEV_SG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 					   dev, 64*1024, 512*1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) }