18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *   US-X2Y AUDIO
48c2ecf20Sopenharmony_ci *   Copyright (c) 2002-2004 by Karsten Wiese
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci *   based on
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci *   (Tentative) USB Audio Driver for ALSA
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *   Main and PCM part
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci *   Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci *   Many codes borrowed from audio.c by
158c2ecf20Sopenharmony_ci *	    Alan Cox (alan@lxorguk.ukuu.org.uk)
168c2ecf20Sopenharmony_ci *	    Thomas Sailer (sailer@ife.ee.ethz.ch)
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
218c2ecf20Sopenharmony_ci#include <linux/slab.h>
228c2ecf20Sopenharmony_ci#include <linux/usb.h>
238c2ecf20Sopenharmony_ci#include <linux/moduleparam.h>
248c2ecf20Sopenharmony_ci#include <sound/core.h>
258c2ecf20Sopenharmony_ci#include <sound/info.h>
268c2ecf20Sopenharmony_ci#include <sound/pcm.h>
278c2ecf20Sopenharmony_ci#include <sound/pcm_params.h>
288c2ecf20Sopenharmony_ci#include "usx2y.h"
298c2ecf20Sopenharmony_ci#include "usbusx2y.h"
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define USX2Y_NRPACKS 4			/* Default value used for nr of packs per urb.
328c2ecf20Sopenharmony_ci					  1 to 4 have been tested ok on uhci.
338c2ecf20Sopenharmony_ci					  To use 3 on ohci, you'd need a patch:
348c2ecf20Sopenharmony_ci					  look for "0000425-linux-2.6.9-rc4-mm1_ohci-hcd.patch.gz" on
358c2ecf20Sopenharmony_ci					  "https://bugtrack.alsa-project.org/alsa-bug/bug_view_page.php?bug_id=0000425"
368c2ecf20Sopenharmony_ci					  .
378c2ecf20Sopenharmony_ci					  1, 2 and 4 work out of the box on ohci, if I recall correctly.
388c2ecf20Sopenharmony_ci					  Bigger is safer operation,
398c2ecf20Sopenharmony_ci					  smaller gives lower latencies.
408c2ecf20Sopenharmony_ci					*/
418c2ecf20Sopenharmony_ci#define USX2Y_NRPACKS_VARIABLE y	/* If your system works ok with this module's parameter
428c2ecf20Sopenharmony_ci					   nrpacks set to 1, you might as well comment
438c2ecf20Sopenharmony_ci					   this #define out, and thereby produce smaller, faster code.
448c2ecf20Sopenharmony_ci					   You'd also set USX2Y_NRPACKS to 1 then.
458c2ecf20Sopenharmony_ci					*/
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#ifdef USX2Y_NRPACKS_VARIABLE
488c2ecf20Sopenharmony_ci static int nrpacks = USX2Y_NRPACKS; /* number of packets per urb */
498c2ecf20Sopenharmony_ci #define  nr_of_packs() nrpacks
508c2ecf20Sopenharmony_ci module_param(nrpacks, int, 0444);
518c2ecf20Sopenharmony_ci MODULE_PARM_DESC(nrpacks, "Number of packets per URB.");
528c2ecf20Sopenharmony_ci#else
538c2ecf20Sopenharmony_ci #define nr_of_packs() USX2Y_NRPACKS
548c2ecf20Sopenharmony_ci#endif
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic int usx2y_urb_capt_retire(struct snd_usx2y_substream *subs)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	struct urb	*urb = subs->completed_urb;
608c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
618c2ecf20Sopenharmony_ci	unsigned char	*cp;
628c2ecf20Sopenharmony_ci	int 		i, len, lens = 0, hwptr_done = subs->hwptr_done;
638c2ecf20Sopenharmony_ci	struct usx2ydev	*usx2y = subs->usx2y;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	for (i = 0; i < nr_of_packs(); i++) {
668c2ecf20Sopenharmony_ci		cp = (unsigned char*)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
678c2ecf20Sopenharmony_ci		if (urb->iso_frame_desc[i].status) { /* active? hmm, skip this */
688c2ecf20Sopenharmony_ci			snd_printk(KERN_ERR "active frame status %i. "
698c2ecf20Sopenharmony_ci				   "Most probably some hardware problem.\n",
708c2ecf20Sopenharmony_ci				   urb->iso_frame_desc[i].status);
718c2ecf20Sopenharmony_ci			return urb->iso_frame_desc[i].status;
728c2ecf20Sopenharmony_ci		}
738c2ecf20Sopenharmony_ci		len = urb->iso_frame_desc[i].actual_length / usx2y->stride;
748c2ecf20Sopenharmony_ci		if (! len) {
758c2ecf20Sopenharmony_ci			snd_printd("0 == len ERROR!\n");
768c2ecf20Sopenharmony_ci			continue;
778c2ecf20Sopenharmony_ci		}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci		/* copy a data chunk */
808c2ecf20Sopenharmony_ci		if ((hwptr_done + len) > runtime->buffer_size) {
818c2ecf20Sopenharmony_ci			int cnt = runtime->buffer_size - hwptr_done;
828c2ecf20Sopenharmony_ci			int blen = cnt * usx2y->stride;
838c2ecf20Sopenharmony_ci			memcpy(runtime->dma_area + hwptr_done * usx2y->stride, cp, blen);
848c2ecf20Sopenharmony_ci			memcpy(runtime->dma_area, cp + blen, len * usx2y->stride - blen);
858c2ecf20Sopenharmony_ci		} else {
868c2ecf20Sopenharmony_ci			memcpy(runtime->dma_area + hwptr_done * usx2y->stride, cp,
878c2ecf20Sopenharmony_ci			       len * usx2y->stride);
888c2ecf20Sopenharmony_ci		}
898c2ecf20Sopenharmony_ci		lens += len;
908c2ecf20Sopenharmony_ci		if ((hwptr_done += len) >= runtime->buffer_size)
918c2ecf20Sopenharmony_ci			hwptr_done -= runtime->buffer_size;
928c2ecf20Sopenharmony_ci	}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	subs->hwptr_done = hwptr_done;
958c2ecf20Sopenharmony_ci	subs->transfer_done += lens;
968c2ecf20Sopenharmony_ci	/* update the pointer, call callback if necessary */
978c2ecf20Sopenharmony_ci	if (subs->transfer_done >= runtime->period_size) {
988c2ecf20Sopenharmony_ci		subs->transfer_done -= runtime->period_size;
998c2ecf20Sopenharmony_ci		snd_pcm_period_elapsed(subs->pcm_substream);
1008c2ecf20Sopenharmony_ci	}
1018c2ecf20Sopenharmony_ci	return 0;
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci/*
1048c2ecf20Sopenharmony_ci * prepare urb for playback data pipe
1058c2ecf20Sopenharmony_ci *
1068c2ecf20Sopenharmony_ci * we copy the data directly from the pcm buffer.
1078c2ecf20Sopenharmony_ci * the current position to be copied is held in hwptr field.
1088c2ecf20Sopenharmony_ci * since a urb can handle only a single linear buffer, if the total
1098c2ecf20Sopenharmony_ci * transferred area overflows the buffer boundary, we cannot send
1108c2ecf20Sopenharmony_ci * it directly from the buffer.  thus the data is once copied to
1118c2ecf20Sopenharmony_ci * a temporary buffer and urb points to that.
1128c2ecf20Sopenharmony_ci */
1138c2ecf20Sopenharmony_cistatic int usx2y_urb_play_prepare(struct snd_usx2y_substream *subs,
1148c2ecf20Sopenharmony_ci				  struct urb *cap_urb,
1158c2ecf20Sopenharmony_ci				  struct urb *urb)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	int count, counts, pack;
1188c2ecf20Sopenharmony_ci	struct usx2ydev *usx2y = subs->usx2y;
1198c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	count = 0;
1228c2ecf20Sopenharmony_ci	for (pack = 0; pack <  nr_of_packs(); pack++) {
1238c2ecf20Sopenharmony_ci		/* calculate the size of a packet */
1248c2ecf20Sopenharmony_ci		counts = cap_urb->iso_frame_desc[pack].actual_length / usx2y->stride;
1258c2ecf20Sopenharmony_ci		count += counts;
1268c2ecf20Sopenharmony_ci		if (counts < 43 || counts > 50) {
1278c2ecf20Sopenharmony_ci			snd_printk(KERN_ERR "should not be here with counts=%i\n", counts);
1288c2ecf20Sopenharmony_ci			return -EPIPE;
1298c2ecf20Sopenharmony_ci		}
1308c2ecf20Sopenharmony_ci		/* set up descriptor */
1318c2ecf20Sopenharmony_ci		urb->iso_frame_desc[pack].offset = pack ?
1328c2ecf20Sopenharmony_ci			urb->iso_frame_desc[pack - 1].offset +
1338c2ecf20Sopenharmony_ci			urb->iso_frame_desc[pack - 1].length :
1348c2ecf20Sopenharmony_ci			0;
1358c2ecf20Sopenharmony_ci		urb->iso_frame_desc[pack].length = cap_urb->iso_frame_desc[pack].actual_length;
1368c2ecf20Sopenharmony_ci	}
1378c2ecf20Sopenharmony_ci	if (atomic_read(&subs->state) >= STATE_PRERUNNING)
1388c2ecf20Sopenharmony_ci		if (subs->hwptr + count > runtime->buffer_size) {
1398c2ecf20Sopenharmony_ci			/* err, the transferred area goes over buffer boundary.
1408c2ecf20Sopenharmony_ci			 * copy the data to the temp buffer.
1418c2ecf20Sopenharmony_ci			 */
1428c2ecf20Sopenharmony_ci			int len;
1438c2ecf20Sopenharmony_ci			len = runtime->buffer_size - subs->hwptr;
1448c2ecf20Sopenharmony_ci			urb->transfer_buffer = subs->tmpbuf;
1458c2ecf20Sopenharmony_ci			memcpy(subs->tmpbuf, runtime->dma_area +
1468c2ecf20Sopenharmony_ci			       subs->hwptr * usx2y->stride, len * usx2y->stride);
1478c2ecf20Sopenharmony_ci			memcpy(subs->tmpbuf + len * usx2y->stride,
1488c2ecf20Sopenharmony_ci			       runtime->dma_area, (count - len) * usx2y->stride);
1498c2ecf20Sopenharmony_ci			subs->hwptr += count;
1508c2ecf20Sopenharmony_ci			subs->hwptr -= runtime->buffer_size;
1518c2ecf20Sopenharmony_ci		} else {
1528c2ecf20Sopenharmony_ci			/* set the buffer pointer */
1538c2ecf20Sopenharmony_ci			urb->transfer_buffer = runtime->dma_area + subs->hwptr * usx2y->stride;
1548c2ecf20Sopenharmony_ci			if ((subs->hwptr += count) >= runtime->buffer_size)
1558c2ecf20Sopenharmony_ci				subs->hwptr -= runtime->buffer_size;
1568c2ecf20Sopenharmony_ci		}
1578c2ecf20Sopenharmony_ci	else
1588c2ecf20Sopenharmony_ci		urb->transfer_buffer = subs->tmpbuf;
1598c2ecf20Sopenharmony_ci	urb->transfer_buffer_length = count * usx2y->stride;
1608c2ecf20Sopenharmony_ci	return 0;
1618c2ecf20Sopenharmony_ci}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci/*
1648c2ecf20Sopenharmony_ci * process after playback data complete
1658c2ecf20Sopenharmony_ci *
1668c2ecf20Sopenharmony_ci * update the current position and call callback if a period is processed.
1678c2ecf20Sopenharmony_ci */
1688c2ecf20Sopenharmony_cistatic void usx2y_urb_play_retire(struct snd_usx2y_substream *subs, struct urb *urb)
1698c2ecf20Sopenharmony_ci{
1708c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime;
1718c2ecf20Sopenharmony_ci	int		len = urb->actual_length / subs->usx2y->stride;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	subs->transfer_done += len;
1748c2ecf20Sopenharmony_ci	subs->hwptr_done +=  len;
1758c2ecf20Sopenharmony_ci	if (subs->hwptr_done >= runtime->buffer_size)
1768c2ecf20Sopenharmony_ci		subs->hwptr_done -= runtime->buffer_size;
1778c2ecf20Sopenharmony_ci	if (subs->transfer_done >= runtime->period_size) {
1788c2ecf20Sopenharmony_ci		subs->transfer_done -= runtime->period_size;
1798c2ecf20Sopenharmony_ci		snd_pcm_period_elapsed(subs->pcm_substream);
1808c2ecf20Sopenharmony_ci	}
1818c2ecf20Sopenharmony_ci}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_cistatic int usx2y_urb_submit(struct snd_usx2y_substream *subs, struct urb *urb, int frame)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	int err;
1868c2ecf20Sopenharmony_ci	if (!urb)
1878c2ecf20Sopenharmony_ci		return -ENODEV;
1888c2ecf20Sopenharmony_ci	urb->start_frame = (frame + NRURBS * nr_of_packs());  // let hcd do rollover sanity checks
1898c2ecf20Sopenharmony_ci	urb->hcpriv = NULL;
1908c2ecf20Sopenharmony_ci	urb->dev = subs->usx2y->dev; /* we need to set this at each time */
1918c2ecf20Sopenharmony_ci	if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
1928c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR "usb_submit_urb() returned %i\n", err);
1938c2ecf20Sopenharmony_ci		return err;
1948c2ecf20Sopenharmony_ci	}
1958c2ecf20Sopenharmony_ci	return 0;
1968c2ecf20Sopenharmony_ci}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_cistatic inline int usx2y_usbframe_complete(struct snd_usx2y_substream *capsubs,
1998c2ecf20Sopenharmony_ci					  struct snd_usx2y_substream *playbacksubs,
2008c2ecf20Sopenharmony_ci					  int frame)
2018c2ecf20Sopenharmony_ci{
2028c2ecf20Sopenharmony_ci	int err, state;
2038c2ecf20Sopenharmony_ci	struct urb *urb = playbacksubs->completed_urb;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	state = atomic_read(&playbacksubs->state);
2068c2ecf20Sopenharmony_ci	if (NULL != urb) {
2078c2ecf20Sopenharmony_ci		if (state == STATE_RUNNING)
2088c2ecf20Sopenharmony_ci			usx2y_urb_play_retire(playbacksubs, urb);
2098c2ecf20Sopenharmony_ci		else if (state >= STATE_PRERUNNING)
2108c2ecf20Sopenharmony_ci			atomic_inc(&playbacksubs->state);
2118c2ecf20Sopenharmony_ci	} else {
2128c2ecf20Sopenharmony_ci		switch (state) {
2138c2ecf20Sopenharmony_ci		case STATE_STARTING1:
2148c2ecf20Sopenharmony_ci			urb = playbacksubs->urb[0];
2158c2ecf20Sopenharmony_ci			atomic_inc(&playbacksubs->state);
2168c2ecf20Sopenharmony_ci			break;
2178c2ecf20Sopenharmony_ci		case STATE_STARTING2:
2188c2ecf20Sopenharmony_ci			urb = playbacksubs->urb[1];
2198c2ecf20Sopenharmony_ci			atomic_inc(&playbacksubs->state);
2208c2ecf20Sopenharmony_ci			break;
2218c2ecf20Sopenharmony_ci		}
2228c2ecf20Sopenharmony_ci	}
2238c2ecf20Sopenharmony_ci	if (urb) {
2248c2ecf20Sopenharmony_ci		if ((err = usx2y_urb_play_prepare(playbacksubs, capsubs->completed_urb, urb)) ||
2258c2ecf20Sopenharmony_ci		    (err = usx2y_urb_submit(playbacksubs, urb, frame))) {
2268c2ecf20Sopenharmony_ci			return err;
2278c2ecf20Sopenharmony_ci		}
2288c2ecf20Sopenharmony_ci	}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	playbacksubs->completed_urb = NULL;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	state = atomic_read(&capsubs->state);
2338c2ecf20Sopenharmony_ci	if (state >= STATE_PREPARED) {
2348c2ecf20Sopenharmony_ci		if (state == STATE_RUNNING) {
2358c2ecf20Sopenharmony_ci			if ((err = usx2y_urb_capt_retire(capsubs)))
2368c2ecf20Sopenharmony_ci				return err;
2378c2ecf20Sopenharmony_ci		} else if (state >= STATE_PRERUNNING)
2388c2ecf20Sopenharmony_ci			atomic_inc(&capsubs->state);
2398c2ecf20Sopenharmony_ci		if ((err = usx2y_urb_submit(capsubs, capsubs->completed_urb, frame)))
2408c2ecf20Sopenharmony_ci			return err;
2418c2ecf20Sopenharmony_ci	}
2428c2ecf20Sopenharmony_ci	capsubs->completed_urb = NULL;
2438c2ecf20Sopenharmony_ci	return 0;
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_cistatic void usx2y_clients_stop(struct usx2ydev *usx2y)
2488c2ecf20Sopenharmony_ci{
2498c2ecf20Sopenharmony_ci	int s, u;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	for (s = 0; s < 4; s++) {
2528c2ecf20Sopenharmony_ci		struct snd_usx2y_substream *subs = usx2y->subs[s];
2538c2ecf20Sopenharmony_ci		if (subs) {
2548c2ecf20Sopenharmony_ci			snd_printdd("%i %p state=%i\n", s, subs, atomic_read(&subs->state));
2558c2ecf20Sopenharmony_ci			atomic_set(&subs->state, STATE_STOPPED);
2568c2ecf20Sopenharmony_ci		}
2578c2ecf20Sopenharmony_ci	}
2588c2ecf20Sopenharmony_ci	for (s = 0; s < 4; s++) {
2598c2ecf20Sopenharmony_ci		struct snd_usx2y_substream *subs = usx2y->subs[s];
2608c2ecf20Sopenharmony_ci		if (subs) {
2618c2ecf20Sopenharmony_ci			if (atomic_read(&subs->state) >= STATE_PRERUNNING)
2628c2ecf20Sopenharmony_ci				snd_pcm_stop_xrun(subs->pcm_substream);
2638c2ecf20Sopenharmony_ci			for (u = 0; u < NRURBS; u++) {
2648c2ecf20Sopenharmony_ci				struct urb *urb = subs->urb[u];
2658c2ecf20Sopenharmony_ci				if (NULL != urb)
2668c2ecf20Sopenharmony_ci					snd_printdd("%i status=%i start_frame=%i\n",
2678c2ecf20Sopenharmony_ci						    u, urb->status, urb->start_frame);
2688c2ecf20Sopenharmony_ci			}
2698c2ecf20Sopenharmony_ci		}
2708c2ecf20Sopenharmony_ci	}
2718c2ecf20Sopenharmony_ci	usx2y->prepare_subs = NULL;
2728c2ecf20Sopenharmony_ci	wake_up(&usx2y->prepare_wait_queue);
2738c2ecf20Sopenharmony_ci}
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_cistatic void usx2y_error_urb_status(struct usx2ydev *usx2y,
2768c2ecf20Sopenharmony_ci				   struct snd_usx2y_substream *subs, struct urb *urb)
2778c2ecf20Sopenharmony_ci{
2788c2ecf20Sopenharmony_ci	snd_printk(KERN_ERR "ep=%i stalled with status=%i\n", subs->endpoint, urb->status);
2798c2ecf20Sopenharmony_ci	urb->status = 0;
2808c2ecf20Sopenharmony_ci	usx2y_clients_stop(usx2y);
2818c2ecf20Sopenharmony_ci}
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_cistatic void i_usx2y_urb_complete(struct urb *urb)
2848c2ecf20Sopenharmony_ci{
2858c2ecf20Sopenharmony_ci	struct snd_usx2y_substream *subs = urb->context;
2868c2ecf20Sopenharmony_ci	struct usx2ydev *usx2y = subs->usx2y;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	if (unlikely(atomic_read(&subs->state) < STATE_PREPARED)) {
2898c2ecf20Sopenharmony_ci		snd_printdd("hcd_frame=%i ep=%i%s status=%i start_frame=%i\n",
2908c2ecf20Sopenharmony_ci			    usb_get_current_frame_number(usx2y->dev),
2918c2ecf20Sopenharmony_ci			    subs->endpoint, usb_pipein(urb->pipe) ? "in" : "out",
2928c2ecf20Sopenharmony_ci			    urb->status, urb->start_frame);
2938c2ecf20Sopenharmony_ci		return;
2948c2ecf20Sopenharmony_ci	}
2958c2ecf20Sopenharmony_ci	if (unlikely(urb->status)) {
2968c2ecf20Sopenharmony_ci		usx2y_error_urb_status(usx2y, subs, urb);
2978c2ecf20Sopenharmony_ci		return;
2988c2ecf20Sopenharmony_ci	}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	subs->completed_urb = urb;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	{
3038c2ecf20Sopenharmony_ci		struct snd_usx2y_substream *capsubs = usx2y->subs[SNDRV_PCM_STREAM_CAPTURE],
3048c2ecf20Sopenharmony_ci			*playbacksubs = usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK];
3058c2ecf20Sopenharmony_ci		if (capsubs->completed_urb &&
3068c2ecf20Sopenharmony_ci		    atomic_read(&capsubs->state) >= STATE_PREPARED &&
3078c2ecf20Sopenharmony_ci		    (playbacksubs->completed_urb ||
3088c2ecf20Sopenharmony_ci		     atomic_read(&playbacksubs->state) < STATE_PREPARED)) {
3098c2ecf20Sopenharmony_ci			if (!usx2y_usbframe_complete(capsubs, playbacksubs, urb->start_frame))
3108c2ecf20Sopenharmony_ci				usx2y->wait_iso_frame += nr_of_packs();
3118c2ecf20Sopenharmony_ci			else {
3128c2ecf20Sopenharmony_ci				snd_printdd("\n");
3138c2ecf20Sopenharmony_ci				usx2y_clients_stop(usx2y);
3148c2ecf20Sopenharmony_ci			}
3158c2ecf20Sopenharmony_ci		}
3168c2ecf20Sopenharmony_ci	}
3178c2ecf20Sopenharmony_ci}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_cistatic void usx2y_urbs_set_complete(struct usx2ydev * usx2y,
3208c2ecf20Sopenharmony_ci				    void (*complete)(struct urb *))
3218c2ecf20Sopenharmony_ci{
3228c2ecf20Sopenharmony_ci	int s, u;
3238c2ecf20Sopenharmony_ci	for (s = 0; s < 4; s++) {
3248c2ecf20Sopenharmony_ci		struct snd_usx2y_substream *subs = usx2y->subs[s];
3258c2ecf20Sopenharmony_ci		if (NULL != subs)
3268c2ecf20Sopenharmony_ci			for (u = 0; u < NRURBS; u++) {
3278c2ecf20Sopenharmony_ci				struct urb * urb = subs->urb[u];
3288c2ecf20Sopenharmony_ci				if (NULL != urb)
3298c2ecf20Sopenharmony_ci					urb->complete = complete;
3308c2ecf20Sopenharmony_ci			}
3318c2ecf20Sopenharmony_ci	}
3328c2ecf20Sopenharmony_ci}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_cistatic void usx2y_subs_startup_finish(struct usx2ydev * usx2y)
3358c2ecf20Sopenharmony_ci{
3368c2ecf20Sopenharmony_ci	usx2y_urbs_set_complete(usx2y, i_usx2y_urb_complete);
3378c2ecf20Sopenharmony_ci	usx2y->prepare_subs = NULL;
3388c2ecf20Sopenharmony_ci}
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_cistatic void i_usx2y_subs_startup(struct urb *urb)
3418c2ecf20Sopenharmony_ci{
3428c2ecf20Sopenharmony_ci	struct snd_usx2y_substream *subs = urb->context;
3438c2ecf20Sopenharmony_ci	struct usx2ydev *usx2y = subs->usx2y;
3448c2ecf20Sopenharmony_ci	struct snd_usx2y_substream *prepare_subs = usx2y->prepare_subs;
3458c2ecf20Sopenharmony_ci	if (NULL != prepare_subs)
3468c2ecf20Sopenharmony_ci		if (urb->start_frame == prepare_subs->urb[0]->start_frame) {
3478c2ecf20Sopenharmony_ci			usx2y_subs_startup_finish(usx2y);
3488c2ecf20Sopenharmony_ci			atomic_inc(&prepare_subs->state);
3498c2ecf20Sopenharmony_ci			wake_up(&usx2y->prepare_wait_queue);
3508c2ecf20Sopenharmony_ci		}
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	i_usx2y_urb_complete(urb);
3538c2ecf20Sopenharmony_ci}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_cistatic void usx2y_subs_prepare(struct snd_usx2y_substream *subs)
3568c2ecf20Sopenharmony_ci{
3578c2ecf20Sopenharmony_ci	snd_printdd("usx2y_substream_prepare(%p) ep=%i urb0=%p urb1=%p\n",
3588c2ecf20Sopenharmony_ci		    subs, subs->endpoint, subs->urb[0], subs->urb[1]);
3598c2ecf20Sopenharmony_ci	/* reset the pointer */
3608c2ecf20Sopenharmony_ci	subs->hwptr = 0;
3618c2ecf20Sopenharmony_ci	subs->hwptr_done = 0;
3628c2ecf20Sopenharmony_ci	subs->transfer_done = 0;
3638c2ecf20Sopenharmony_ci}
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_cistatic void usx2y_urb_release(struct urb **urb, int free_tb)
3678c2ecf20Sopenharmony_ci{
3688c2ecf20Sopenharmony_ci	if (*urb) {
3698c2ecf20Sopenharmony_ci		usb_kill_urb(*urb);
3708c2ecf20Sopenharmony_ci		if (free_tb)
3718c2ecf20Sopenharmony_ci			kfree((*urb)->transfer_buffer);
3728c2ecf20Sopenharmony_ci		usb_free_urb(*urb);
3738c2ecf20Sopenharmony_ci		*urb = NULL;
3748c2ecf20Sopenharmony_ci	}
3758c2ecf20Sopenharmony_ci}
3768c2ecf20Sopenharmony_ci/*
3778c2ecf20Sopenharmony_ci * release a substreams urbs
3788c2ecf20Sopenharmony_ci */
3798c2ecf20Sopenharmony_cistatic void usx2y_urbs_release(struct snd_usx2y_substream *subs)
3808c2ecf20Sopenharmony_ci{
3818c2ecf20Sopenharmony_ci	int i;
3828c2ecf20Sopenharmony_ci	snd_printdd("usx2y_urbs_release() %i\n", subs->endpoint);
3838c2ecf20Sopenharmony_ci	for (i = 0; i < NRURBS; i++)
3848c2ecf20Sopenharmony_ci		usx2y_urb_release(subs->urb + i,
3858c2ecf20Sopenharmony_ci				  subs != subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK]);
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	kfree(subs->tmpbuf);
3888c2ecf20Sopenharmony_ci	subs->tmpbuf = NULL;
3898c2ecf20Sopenharmony_ci}
3908c2ecf20Sopenharmony_ci/*
3918c2ecf20Sopenharmony_ci * initialize a substream's urbs
3928c2ecf20Sopenharmony_ci */
3938c2ecf20Sopenharmony_cistatic int usx2y_urbs_allocate(struct snd_usx2y_substream *subs)
3948c2ecf20Sopenharmony_ci{
3958c2ecf20Sopenharmony_ci	int i;
3968c2ecf20Sopenharmony_ci	unsigned int pipe;
3978c2ecf20Sopenharmony_ci	int is_playback = subs == subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK];
3988c2ecf20Sopenharmony_ci	struct usb_device *dev = subs->usx2y->dev;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	pipe = is_playback ? usb_sndisocpipe(dev, subs->endpoint) :
4018c2ecf20Sopenharmony_ci			usb_rcvisocpipe(dev, subs->endpoint);
4028c2ecf20Sopenharmony_ci	subs->maxpacksize = usb_maxpacket(dev, pipe, is_playback);
4038c2ecf20Sopenharmony_ci	if (!subs->maxpacksize)
4048c2ecf20Sopenharmony_ci		return -EINVAL;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	if (is_playback && NULL == subs->tmpbuf) {	/* allocate a temporary buffer for playback */
4078c2ecf20Sopenharmony_ci		subs->tmpbuf = kcalloc(nr_of_packs(), subs->maxpacksize, GFP_KERNEL);
4088c2ecf20Sopenharmony_ci		if (!subs->tmpbuf)
4098c2ecf20Sopenharmony_ci			return -ENOMEM;
4108c2ecf20Sopenharmony_ci	}
4118c2ecf20Sopenharmony_ci	/* allocate and initialize data urbs */
4128c2ecf20Sopenharmony_ci	for (i = 0; i < NRURBS; i++) {
4138c2ecf20Sopenharmony_ci		struct urb **purb = subs->urb + i;
4148c2ecf20Sopenharmony_ci		if (*purb) {
4158c2ecf20Sopenharmony_ci			usb_kill_urb(*purb);
4168c2ecf20Sopenharmony_ci			continue;
4178c2ecf20Sopenharmony_ci		}
4188c2ecf20Sopenharmony_ci		*purb = usb_alloc_urb(nr_of_packs(), GFP_KERNEL);
4198c2ecf20Sopenharmony_ci		if (NULL == *purb) {
4208c2ecf20Sopenharmony_ci			usx2y_urbs_release(subs);
4218c2ecf20Sopenharmony_ci			return -ENOMEM;
4228c2ecf20Sopenharmony_ci		}
4238c2ecf20Sopenharmony_ci		if (!is_playback && !(*purb)->transfer_buffer) {
4248c2ecf20Sopenharmony_ci			/* allocate a capture buffer per urb */
4258c2ecf20Sopenharmony_ci			(*purb)->transfer_buffer =
4268c2ecf20Sopenharmony_ci				kmalloc_array(subs->maxpacksize,
4278c2ecf20Sopenharmony_ci					      nr_of_packs(), GFP_KERNEL);
4288c2ecf20Sopenharmony_ci			if (NULL == (*purb)->transfer_buffer) {
4298c2ecf20Sopenharmony_ci				usx2y_urbs_release(subs);
4308c2ecf20Sopenharmony_ci				return -ENOMEM;
4318c2ecf20Sopenharmony_ci			}
4328c2ecf20Sopenharmony_ci		}
4338c2ecf20Sopenharmony_ci		(*purb)->dev = dev;
4348c2ecf20Sopenharmony_ci		(*purb)->pipe = pipe;
4358c2ecf20Sopenharmony_ci		(*purb)->number_of_packets = nr_of_packs();
4368c2ecf20Sopenharmony_ci		(*purb)->context = subs;
4378c2ecf20Sopenharmony_ci		(*purb)->interval = 1;
4388c2ecf20Sopenharmony_ci		(*purb)->complete = i_usx2y_subs_startup;
4398c2ecf20Sopenharmony_ci	}
4408c2ecf20Sopenharmony_ci	return 0;
4418c2ecf20Sopenharmony_ci}
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_cistatic void usx2y_subs_startup(struct snd_usx2y_substream *subs)
4448c2ecf20Sopenharmony_ci{
4458c2ecf20Sopenharmony_ci	struct usx2ydev *usx2y = subs->usx2y;
4468c2ecf20Sopenharmony_ci	usx2y->prepare_subs = subs;
4478c2ecf20Sopenharmony_ci	subs->urb[0]->start_frame = -1;
4488c2ecf20Sopenharmony_ci	wmb();
4498c2ecf20Sopenharmony_ci	usx2y_urbs_set_complete(usx2y, i_usx2y_subs_startup);
4508c2ecf20Sopenharmony_ci}
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_cistatic int usx2y_urbs_start(struct snd_usx2y_substream *subs)
4538c2ecf20Sopenharmony_ci{
4548c2ecf20Sopenharmony_ci	int i, err;
4558c2ecf20Sopenharmony_ci	struct usx2ydev *usx2y = subs->usx2y;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	if ((err = usx2y_urbs_allocate(subs)) < 0)
4588c2ecf20Sopenharmony_ci		return err;
4598c2ecf20Sopenharmony_ci	subs->completed_urb = NULL;
4608c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++) {
4618c2ecf20Sopenharmony_ci		struct snd_usx2y_substream *subs = usx2y->subs[i];
4628c2ecf20Sopenharmony_ci		if (subs != NULL && atomic_read(&subs->state) >= STATE_PREPARED)
4638c2ecf20Sopenharmony_ci			goto start;
4648c2ecf20Sopenharmony_ci	}
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci start:
4678c2ecf20Sopenharmony_ci	usx2y_subs_startup(subs);
4688c2ecf20Sopenharmony_ci	for (i = 0; i < NRURBS; i++) {
4698c2ecf20Sopenharmony_ci		struct urb *urb = subs->urb[i];
4708c2ecf20Sopenharmony_ci		if (usb_pipein(urb->pipe)) {
4718c2ecf20Sopenharmony_ci			unsigned long pack;
4728c2ecf20Sopenharmony_ci			if (0 == i)
4738c2ecf20Sopenharmony_ci				atomic_set(&subs->state, STATE_STARTING3);
4748c2ecf20Sopenharmony_ci			urb->dev = usx2y->dev;
4758c2ecf20Sopenharmony_ci			for (pack = 0; pack < nr_of_packs(); pack++) {
4768c2ecf20Sopenharmony_ci				urb->iso_frame_desc[pack].offset = subs->maxpacksize * pack;
4778c2ecf20Sopenharmony_ci				urb->iso_frame_desc[pack].length = subs->maxpacksize;
4788c2ecf20Sopenharmony_ci			}
4798c2ecf20Sopenharmony_ci			urb->transfer_buffer_length = subs->maxpacksize * nr_of_packs();
4808c2ecf20Sopenharmony_ci			if ((err = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
4818c2ecf20Sopenharmony_ci				snd_printk (KERN_ERR "cannot submit datapipe for urb %d, err = %d\n", i, err);
4828c2ecf20Sopenharmony_ci				err = -EPIPE;
4838c2ecf20Sopenharmony_ci				goto cleanup;
4848c2ecf20Sopenharmony_ci			} else
4858c2ecf20Sopenharmony_ci				if (i == 0)
4868c2ecf20Sopenharmony_ci					usx2y->wait_iso_frame = urb->start_frame;
4878c2ecf20Sopenharmony_ci			urb->transfer_flags = 0;
4888c2ecf20Sopenharmony_ci		} else {
4898c2ecf20Sopenharmony_ci			atomic_set(&subs->state, STATE_STARTING1);
4908c2ecf20Sopenharmony_ci			break;
4918c2ecf20Sopenharmony_ci		}
4928c2ecf20Sopenharmony_ci	}
4938c2ecf20Sopenharmony_ci	err = 0;
4948c2ecf20Sopenharmony_ci	wait_event(usx2y->prepare_wait_queue, NULL == usx2y->prepare_subs);
4958c2ecf20Sopenharmony_ci	if (atomic_read(&subs->state) != STATE_PREPARED)
4968c2ecf20Sopenharmony_ci		err = -EPIPE;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci cleanup:
4998c2ecf20Sopenharmony_ci	if (err) {
5008c2ecf20Sopenharmony_ci		usx2y_subs_startup_finish(usx2y);
5018c2ecf20Sopenharmony_ci		usx2y_clients_stop(usx2y);		// something is completely wroong > stop evrything
5028c2ecf20Sopenharmony_ci	}
5038c2ecf20Sopenharmony_ci	return err;
5048c2ecf20Sopenharmony_ci}
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci/*
5078c2ecf20Sopenharmony_ci * return the current pcm pointer.  just return the hwptr_done value.
5088c2ecf20Sopenharmony_ci */
5098c2ecf20Sopenharmony_cistatic snd_pcm_uframes_t snd_usx2y_pcm_pointer(struct snd_pcm_substream *substream)
5108c2ecf20Sopenharmony_ci{
5118c2ecf20Sopenharmony_ci	struct snd_usx2y_substream *subs = substream->runtime->private_data;
5128c2ecf20Sopenharmony_ci	return subs->hwptr_done;
5138c2ecf20Sopenharmony_ci}
5148c2ecf20Sopenharmony_ci/*
5158c2ecf20Sopenharmony_ci * start/stop substream
5168c2ecf20Sopenharmony_ci */
5178c2ecf20Sopenharmony_cistatic int snd_usx2y_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
5188c2ecf20Sopenharmony_ci{
5198c2ecf20Sopenharmony_ci	struct snd_usx2y_substream *subs = substream->runtime->private_data;
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	switch (cmd) {
5228c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_START:
5238c2ecf20Sopenharmony_ci		snd_printdd("snd_usx2y_pcm_trigger(START)\n");
5248c2ecf20Sopenharmony_ci		if (atomic_read(&subs->state) == STATE_PREPARED &&
5258c2ecf20Sopenharmony_ci		    atomic_read(&subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE]->state) >= STATE_PREPARED) {
5268c2ecf20Sopenharmony_ci			atomic_set(&subs->state, STATE_PRERUNNING);
5278c2ecf20Sopenharmony_ci		} else {
5288c2ecf20Sopenharmony_ci			snd_printdd("\n");
5298c2ecf20Sopenharmony_ci			return -EPIPE;
5308c2ecf20Sopenharmony_ci		}
5318c2ecf20Sopenharmony_ci		break;
5328c2ecf20Sopenharmony_ci	case SNDRV_PCM_TRIGGER_STOP:
5338c2ecf20Sopenharmony_ci		snd_printdd("snd_usx2y_pcm_trigger(STOP)\n");
5348c2ecf20Sopenharmony_ci		if (atomic_read(&subs->state) >= STATE_PRERUNNING)
5358c2ecf20Sopenharmony_ci			atomic_set(&subs->state, STATE_PREPARED);
5368c2ecf20Sopenharmony_ci		break;
5378c2ecf20Sopenharmony_ci	default:
5388c2ecf20Sopenharmony_ci		return -EINVAL;
5398c2ecf20Sopenharmony_ci	}
5408c2ecf20Sopenharmony_ci	return 0;
5418c2ecf20Sopenharmony_ci}
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci/*
5458c2ecf20Sopenharmony_ci * allocate a buffer, setup samplerate
5468c2ecf20Sopenharmony_ci *
5478c2ecf20Sopenharmony_ci * so far we use a physically linear buffer although packetize transfer
5488c2ecf20Sopenharmony_ci * doesn't need a continuous area.
5498c2ecf20Sopenharmony_ci * if sg buffer is supported on the later version of alsa, we'll follow
5508c2ecf20Sopenharmony_ci * that.
5518c2ecf20Sopenharmony_ci */
5528c2ecf20Sopenharmony_cistatic const struct s_c2
5538c2ecf20Sopenharmony_ci{
5548c2ecf20Sopenharmony_ci	char c1, c2;
5558c2ecf20Sopenharmony_ci}
5568c2ecf20Sopenharmony_ci	setrate_44100[] =
5578c2ecf20Sopenharmony_ci{
5588c2ecf20Sopenharmony_ci	{ 0x14, 0x08},	// this line sets 44100, well actually a little less
5598c2ecf20Sopenharmony_ci	{ 0x18, 0x40},	// only tascam / frontier design knows the further lines .......
5608c2ecf20Sopenharmony_ci	{ 0x18, 0x42},
5618c2ecf20Sopenharmony_ci	{ 0x18, 0x45},
5628c2ecf20Sopenharmony_ci	{ 0x18, 0x46},
5638c2ecf20Sopenharmony_ci	{ 0x18, 0x48},
5648c2ecf20Sopenharmony_ci	{ 0x18, 0x4A},
5658c2ecf20Sopenharmony_ci	{ 0x18, 0x4C},
5668c2ecf20Sopenharmony_ci	{ 0x18, 0x4E},
5678c2ecf20Sopenharmony_ci	{ 0x18, 0x50},
5688c2ecf20Sopenharmony_ci	{ 0x18, 0x52},
5698c2ecf20Sopenharmony_ci	{ 0x18, 0x54},
5708c2ecf20Sopenharmony_ci	{ 0x18, 0x56},
5718c2ecf20Sopenharmony_ci	{ 0x18, 0x58},
5728c2ecf20Sopenharmony_ci	{ 0x18, 0x5A},
5738c2ecf20Sopenharmony_ci	{ 0x18, 0x5C},
5748c2ecf20Sopenharmony_ci	{ 0x18, 0x5E},
5758c2ecf20Sopenharmony_ci	{ 0x18, 0x60},
5768c2ecf20Sopenharmony_ci	{ 0x18, 0x62},
5778c2ecf20Sopenharmony_ci	{ 0x18, 0x64},
5788c2ecf20Sopenharmony_ci	{ 0x18, 0x66},
5798c2ecf20Sopenharmony_ci	{ 0x18, 0x68},
5808c2ecf20Sopenharmony_ci	{ 0x18, 0x6A},
5818c2ecf20Sopenharmony_ci	{ 0x18, 0x6C},
5828c2ecf20Sopenharmony_ci	{ 0x18, 0x6E},
5838c2ecf20Sopenharmony_ci	{ 0x18, 0x70},
5848c2ecf20Sopenharmony_ci	{ 0x18, 0x72},
5858c2ecf20Sopenharmony_ci	{ 0x18, 0x74},
5868c2ecf20Sopenharmony_ci	{ 0x18, 0x76},
5878c2ecf20Sopenharmony_ci	{ 0x18, 0x78},
5888c2ecf20Sopenharmony_ci	{ 0x18, 0x7A},
5898c2ecf20Sopenharmony_ci	{ 0x18, 0x7C},
5908c2ecf20Sopenharmony_ci	{ 0x18, 0x7E}
5918c2ecf20Sopenharmony_ci};
5928c2ecf20Sopenharmony_cistatic const struct s_c2 setrate_48000[] =
5938c2ecf20Sopenharmony_ci{
5948c2ecf20Sopenharmony_ci	{ 0x14, 0x09},	// this line sets 48000, well actually a little less
5958c2ecf20Sopenharmony_ci	{ 0x18, 0x40},	// only tascam / frontier design knows the further lines .......
5968c2ecf20Sopenharmony_ci	{ 0x18, 0x42},
5978c2ecf20Sopenharmony_ci	{ 0x18, 0x45},
5988c2ecf20Sopenharmony_ci	{ 0x18, 0x46},
5998c2ecf20Sopenharmony_ci	{ 0x18, 0x48},
6008c2ecf20Sopenharmony_ci	{ 0x18, 0x4A},
6018c2ecf20Sopenharmony_ci	{ 0x18, 0x4C},
6028c2ecf20Sopenharmony_ci	{ 0x18, 0x4E},
6038c2ecf20Sopenharmony_ci	{ 0x18, 0x50},
6048c2ecf20Sopenharmony_ci	{ 0x18, 0x52},
6058c2ecf20Sopenharmony_ci	{ 0x18, 0x54},
6068c2ecf20Sopenharmony_ci	{ 0x18, 0x56},
6078c2ecf20Sopenharmony_ci	{ 0x18, 0x58},
6088c2ecf20Sopenharmony_ci	{ 0x18, 0x5A},
6098c2ecf20Sopenharmony_ci	{ 0x18, 0x5C},
6108c2ecf20Sopenharmony_ci	{ 0x18, 0x5E},
6118c2ecf20Sopenharmony_ci	{ 0x18, 0x60},
6128c2ecf20Sopenharmony_ci	{ 0x18, 0x62},
6138c2ecf20Sopenharmony_ci	{ 0x18, 0x64},
6148c2ecf20Sopenharmony_ci	{ 0x18, 0x66},
6158c2ecf20Sopenharmony_ci	{ 0x18, 0x68},
6168c2ecf20Sopenharmony_ci	{ 0x18, 0x6A},
6178c2ecf20Sopenharmony_ci	{ 0x18, 0x6C},
6188c2ecf20Sopenharmony_ci	{ 0x18, 0x6E},
6198c2ecf20Sopenharmony_ci	{ 0x18, 0x70},
6208c2ecf20Sopenharmony_ci	{ 0x18, 0x73},
6218c2ecf20Sopenharmony_ci	{ 0x18, 0x74},
6228c2ecf20Sopenharmony_ci	{ 0x18, 0x76},
6238c2ecf20Sopenharmony_ci	{ 0x18, 0x78},
6248c2ecf20Sopenharmony_ci	{ 0x18, 0x7A},
6258c2ecf20Sopenharmony_ci	{ 0x18, 0x7C},
6268c2ecf20Sopenharmony_ci	{ 0x18, 0x7E}
6278c2ecf20Sopenharmony_ci};
6288c2ecf20Sopenharmony_ci#define NOOF_SETRATE_URBS ARRAY_SIZE(setrate_48000)
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_cistatic void i_usx2y_04int(struct urb *urb)
6318c2ecf20Sopenharmony_ci{
6328c2ecf20Sopenharmony_ci	struct usx2ydev *usx2y = urb->context;
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	if (urb->status)
6358c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR "snd_usx2y_04int() urb->status=%i\n", urb->status);
6368c2ecf20Sopenharmony_ci	if (0 == --usx2y->us04->len)
6378c2ecf20Sopenharmony_ci		wake_up(&usx2y->in04_wait_queue);
6388c2ecf20Sopenharmony_ci}
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_cistatic int usx2y_rate_set(struct usx2ydev *usx2y, int rate)
6418c2ecf20Sopenharmony_ci{
6428c2ecf20Sopenharmony_ci	int			err = 0, i;
6438c2ecf20Sopenharmony_ci	struct snd_usx2y_urb_seq	*us = NULL;
6448c2ecf20Sopenharmony_ci	int			*usbdata = NULL;
6458c2ecf20Sopenharmony_ci	const struct s_c2	*ra = rate == 48000 ? setrate_48000 : setrate_44100;
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci	if (usx2y->rate != rate) {
6488c2ecf20Sopenharmony_ci		us = kzalloc(sizeof(*us) + sizeof(struct urb*) * NOOF_SETRATE_URBS, GFP_KERNEL);
6498c2ecf20Sopenharmony_ci		if (NULL == us) {
6508c2ecf20Sopenharmony_ci			err = -ENOMEM;
6518c2ecf20Sopenharmony_ci			goto cleanup;
6528c2ecf20Sopenharmony_ci		}
6538c2ecf20Sopenharmony_ci		usbdata = kmalloc_array(NOOF_SETRATE_URBS, sizeof(int),
6548c2ecf20Sopenharmony_ci					GFP_KERNEL);
6558c2ecf20Sopenharmony_ci		if (NULL == usbdata) {
6568c2ecf20Sopenharmony_ci			err = -ENOMEM;
6578c2ecf20Sopenharmony_ci			goto cleanup;
6588c2ecf20Sopenharmony_ci		}
6598c2ecf20Sopenharmony_ci		for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
6608c2ecf20Sopenharmony_ci			if (NULL == (us->urb[i] = usb_alloc_urb(0, GFP_KERNEL))) {
6618c2ecf20Sopenharmony_ci				err = -ENOMEM;
6628c2ecf20Sopenharmony_ci				goto cleanup;
6638c2ecf20Sopenharmony_ci			}
6648c2ecf20Sopenharmony_ci			((char*)(usbdata + i))[0] = ra[i].c1;
6658c2ecf20Sopenharmony_ci			((char*)(usbdata + i))[1] = ra[i].c2;
6668c2ecf20Sopenharmony_ci			usb_fill_bulk_urb(us->urb[i], usx2y->dev, usb_sndbulkpipe(usx2y->dev, 4),
6678c2ecf20Sopenharmony_ci					  usbdata + i, 2, i_usx2y_04int, usx2y);
6688c2ecf20Sopenharmony_ci		}
6698c2ecf20Sopenharmony_ci		err = usb_urb_ep_type_check(us->urb[0]);
6708c2ecf20Sopenharmony_ci		if (err < 0)
6718c2ecf20Sopenharmony_ci			goto cleanup;
6728c2ecf20Sopenharmony_ci		us->submitted =	0;
6738c2ecf20Sopenharmony_ci		us->len =	NOOF_SETRATE_URBS;
6748c2ecf20Sopenharmony_ci		usx2y->us04 =	us;
6758c2ecf20Sopenharmony_ci		wait_event_timeout(usx2y->in04_wait_queue, 0 == us->len, HZ);
6768c2ecf20Sopenharmony_ci		usx2y->us04 =	NULL;
6778c2ecf20Sopenharmony_ci		if (us->len)
6788c2ecf20Sopenharmony_ci			err = -ENODEV;
6798c2ecf20Sopenharmony_ci	cleanup:
6808c2ecf20Sopenharmony_ci		if (us) {
6818c2ecf20Sopenharmony_ci			us->submitted =	2*NOOF_SETRATE_URBS;
6828c2ecf20Sopenharmony_ci			for (i = 0; i < NOOF_SETRATE_URBS; ++i) {
6838c2ecf20Sopenharmony_ci				struct urb *urb = us->urb[i];
6848c2ecf20Sopenharmony_ci				if (!urb)
6858c2ecf20Sopenharmony_ci					continue;
6868c2ecf20Sopenharmony_ci				if (urb->status) {
6878c2ecf20Sopenharmony_ci					if (!err)
6888c2ecf20Sopenharmony_ci						err = -ENODEV;
6898c2ecf20Sopenharmony_ci					usb_kill_urb(urb);
6908c2ecf20Sopenharmony_ci				}
6918c2ecf20Sopenharmony_ci				usb_free_urb(urb);
6928c2ecf20Sopenharmony_ci			}
6938c2ecf20Sopenharmony_ci			usx2y->us04 = NULL;
6948c2ecf20Sopenharmony_ci			kfree(usbdata);
6958c2ecf20Sopenharmony_ci			kfree(us);
6968c2ecf20Sopenharmony_ci			if (!err)
6978c2ecf20Sopenharmony_ci				usx2y->rate = rate;
6988c2ecf20Sopenharmony_ci		}
6998c2ecf20Sopenharmony_ci	}
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci	return err;
7028c2ecf20Sopenharmony_ci}
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_cistatic int usx2y_format_set(struct usx2ydev *usx2y, snd_pcm_format_t format)
7068c2ecf20Sopenharmony_ci{
7078c2ecf20Sopenharmony_ci	int alternate, err;
7088c2ecf20Sopenharmony_ci	struct list_head* p;
7098c2ecf20Sopenharmony_ci	if (format == SNDRV_PCM_FORMAT_S24_3LE) {
7108c2ecf20Sopenharmony_ci		alternate = 2;
7118c2ecf20Sopenharmony_ci		usx2y->stride = 6;
7128c2ecf20Sopenharmony_ci	} else {
7138c2ecf20Sopenharmony_ci		alternate = 1;
7148c2ecf20Sopenharmony_ci		usx2y->stride = 4;
7158c2ecf20Sopenharmony_ci	}
7168c2ecf20Sopenharmony_ci	list_for_each(p, &usx2y->midi_list) {
7178c2ecf20Sopenharmony_ci		snd_usbmidi_input_stop(p);
7188c2ecf20Sopenharmony_ci	}
7198c2ecf20Sopenharmony_ci	usb_kill_urb(usx2y->in04_urb);
7208c2ecf20Sopenharmony_ci	if ((err = usb_set_interface(usx2y->dev, 0, alternate))) {
7218c2ecf20Sopenharmony_ci		snd_printk(KERN_ERR "usb_set_interface error \n");
7228c2ecf20Sopenharmony_ci		return err;
7238c2ecf20Sopenharmony_ci	}
7248c2ecf20Sopenharmony_ci	usx2y->in04_urb->dev = usx2y->dev;
7258c2ecf20Sopenharmony_ci	err = usb_submit_urb(usx2y->in04_urb, GFP_KERNEL);
7268c2ecf20Sopenharmony_ci	list_for_each(p, &usx2y->midi_list) {
7278c2ecf20Sopenharmony_ci		snd_usbmidi_input_start(p);
7288c2ecf20Sopenharmony_ci	}
7298c2ecf20Sopenharmony_ci	usx2y->format = format;
7308c2ecf20Sopenharmony_ci	usx2y->rate = 0;
7318c2ecf20Sopenharmony_ci	return err;
7328c2ecf20Sopenharmony_ci}
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_cistatic int snd_usx2y_pcm_hw_params(struct snd_pcm_substream *substream,
7368c2ecf20Sopenharmony_ci				   struct snd_pcm_hw_params *hw_params)
7378c2ecf20Sopenharmony_ci{
7388c2ecf20Sopenharmony_ci	int			err = 0;
7398c2ecf20Sopenharmony_ci	unsigned int		rate = params_rate(hw_params);
7408c2ecf20Sopenharmony_ci	snd_pcm_format_t	format = params_format(hw_params);
7418c2ecf20Sopenharmony_ci	struct snd_card *card = substream->pstr->pcm->card;
7428c2ecf20Sopenharmony_ci	struct usx2ydev	*dev = usx2y(card);
7438c2ecf20Sopenharmony_ci	int i;
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci	mutex_lock(&usx2y(card)->pcm_mutex);
7468c2ecf20Sopenharmony_ci	snd_printdd("snd_usx2y_hw_params(%p, %p)\n", substream, hw_params);
7478c2ecf20Sopenharmony_ci	/* all pcm substreams off one usx2y have to operate at the same
7488c2ecf20Sopenharmony_ci	 * rate & format
7498c2ecf20Sopenharmony_ci	 */
7508c2ecf20Sopenharmony_ci	for (i = 0; i < dev->pcm_devs * 2; i++) {
7518c2ecf20Sopenharmony_ci		struct snd_usx2y_substream *subs = dev->subs[i];
7528c2ecf20Sopenharmony_ci		struct snd_pcm_substream *test_substream;
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci		if (!subs)
7558c2ecf20Sopenharmony_ci			continue;
7568c2ecf20Sopenharmony_ci		test_substream = subs->pcm_substream;
7578c2ecf20Sopenharmony_ci		if (!test_substream || test_substream == substream ||
7588c2ecf20Sopenharmony_ci		    !test_substream->runtime)
7598c2ecf20Sopenharmony_ci			continue;
7608c2ecf20Sopenharmony_ci		if ((test_substream->runtime->format &&
7618c2ecf20Sopenharmony_ci		     test_substream->runtime->format != format) ||
7628c2ecf20Sopenharmony_ci		    (test_substream->runtime->rate &&
7638c2ecf20Sopenharmony_ci		     test_substream->runtime->rate != rate)) {
7648c2ecf20Sopenharmony_ci			err = -EINVAL;
7658c2ecf20Sopenharmony_ci			goto error;
7668c2ecf20Sopenharmony_ci		}
7678c2ecf20Sopenharmony_ci	}
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci error:
7708c2ecf20Sopenharmony_ci	mutex_unlock(&usx2y(card)->pcm_mutex);
7718c2ecf20Sopenharmony_ci	return err;
7728c2ecf20Sopenharmony_ci}
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci/*
7758c2ecf20Sopenharmony_ci * free the buffer
7768c2ecf20Sopenharmony_ci */
7778c2ecf20Sopenharmony_cistatic int snd_usx2y_pcm_hw_free(struct snd_pcm_substream *substream)
7788c2ecf20Sopenharmony_ci{
7798c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
7808c2ecf20Sopenharmony_ci	struct snd_usx2y_substream *subs = runtime->private_data;
7818c2ecf20Sopenharmony_ci	mutex_lock(&subs->usx2y->pcm_mutex);
7828c2ecf20Sopenharmony_ci	snd_printdd("snd_usx2y_hw_free(%p)\n", substream);
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci	if (SNDRV_PCM_STREAM_PLAYBACK == substream->stream) {
7858c2ecf20Sopenharmony_ci		struct snd_usx2y_substream *cap_subs = subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE];
7868c2ecf20Sopenharmony_ci		atomic_set(&subs->state, STATE_STOPPED);
7878c2ecf20Sopenharmony_ci		usx2y_urbs_release(subs);
7888c2ecf20Sopenharmony_ci		if (!cap_subs->pcm_substream ||
7898c2ecf20Sopenharmony_ci		    !cap_subs->pcm_substream->runtime ||
7908c2ecf20Sopenharmony_ci		    !cap_subs->pcm_substream->runtime->status ||
7918c2ecf20Sopenharmony_ci		    cap_subs->pcm_substream->runtime->status->state < SNDRV_PCM_STATE_PREPARED) {
7928c2ecf20Sopenharmony_ci			atomic_set(&cap_subs->state, STATE_STOPPED);
7938c2ecf20Sopenharmony_ci			usx2y_urbs_release(cap_subs);
7948c2ecf20Sopenharmony_ci		}
7958c2ecf20Sopenharmony_ci	} else {
7968c2ecf20Sopenharmony_ci		struct snd_usx2y_substream *playback_subs = subs->usx2y->subs[SNDRV_PCM_STREAM_PLAYBACK];
7978c2ecf20Sopenharmony_ci		if (atomic_read(&playback_subs->state) < STATE_PREPARED) {
7988c2ecf20Sopenharmony_ci			atomic_set(&subs->state, STATE_STOPPED);
7998c2ecf20Sopenharmony_ci			usx2y_urbs_release(subs);
8008c2ecf20Sopenharmony_ci		}
8018c2ecf20Sopenharmony_ci	}
8028c2ecf20Sopenharmony_ci	mutex_unlock(&subs->usx2y->pcm_mutex);
8038c2ecf20Sopenharmony_ci	return 0;
8048c2ecf20Sopenharmony_ci}
8058c2ecf20Sopenharmony_ci/*
8068c2ecf20Sopenharmony_ci * prepare callback
8078c2ecf20Sopenharmony_ci *
8088c2ecf20Sopenharmony_ci * set format and initialize urbs
8098c2ecf20Sopenharmony_ci */
8108c2ecf20Sopenharmony_cistatic int snd_usx2y_pcm_prepare(struct snd_pcm_substream *substream)
8118c2ecf20Sopenharmony_ci{
8128c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
8138c2ecf20Sopenharmony_ci	struct snd_usx2y_substream *subs = runtime->private_data;
8148c2ecf20Sopenharmony_ci	struct usx2ydev *usx2y = subs->usx2y;
8158c2ecf20Sopenharmony_ci	struct snd_usx2y_substream *capsubs = subs->usx2y->subs[SNDRV_PCM_STREAM_CAPTURE];
8168c2ecf20Sopenharmony_ci	int err = 0;
8178c2ecf20Sopenharmony_ci	snd_printdd("snd_usx2y_pcm_prepare(%p)\n", substream);
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci	mutex_lock(&usx2y->pcm_mutex);
8208c2ecf20Sopenharmony_ci	usx2y_subs_prepare(subs);
8218c2ecf20Sopenharmony_ci// Start hardware streams
8228c2ecf20Sopenharmony_ci// SyncStream first....
8238c2ecf20Sopenharmony_ci	if (atomic_read(&capsubs->state) < STATE_PREPARED) {
8248c2ecf20Sopenharmony_ci		if (usx2y->format != runtime->format)
8258c2ecf20Sopenharmony_ci			if ((err = usx2y_format_set(usx2y, runtime->format)) < 0)
8268c2ecf20Sopenharmony_ci				goto up_prepare_mutex;
8278c2ecf20Sopenharmony_ci		if (usx2y->rate != runtime->rate)
8288c2ecf20Sopenharmony_ci			if ((err = usx2y_rate_set(usx2y, runtime->rate)) < 0)
8298c2ecf20Sopenharmony_ci				goto up_prepare_mutex;
8308c2ecf20Sopenharmony_ci		snd_printdd("starting capture pipe for %s\n", subs == capsubs ? "self" : "playpipe");
8318c2ecf20Sopenharmony_ci		if (0 > (err = usx2y_urbs_start(capsubs)))
8328c2ecf20Sopenharmony_ci			goto up_prepare_mutex;
8338c2ecf20Sopenharmony_ci	}
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci	if (subs != capsubs && atomic_read(&subs->state) < STATE_PREPARED)
8368c2ecf20Sopenharmony_ci		err = usx2y_urbs_start(subs);
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci up_prepare_mutex:
8398c2ecf20Sopenharmony_ci	mutex_unlock(&usx2y->pcm_mutex);
8408c2ecf20Sopenharmony_ci	return err;
8418c2ecf20Sopenharmony_ci}
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_cistatic const struct snd_pcm_hardware snd_usx2y_2c =
8448c2ecf20Sopenharmony_ci{
8458c2ecf20Sopenharmony_ci	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
8468c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
8478c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_MMAP_VALID |
8488c2ecf20Sopenharmony_ci				 SNDRV_PCM_INFO_BATCH),
8498c2ecf20Sopenharmony_ci	.formats =                 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE,
8508c2ecf20Sopenharmony_ci	.rates =                   SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
8518c2ecf20Sopenharmony_ci	.rate_min =                44100,
8528c2ecf20Sopenharmony_ci	.rate_max =                48000,
8538c2ecf20Sopenharmony_ci	.channels_min =            2,
8548c2ecf20Sopenharmony_ci	.channels_max =            2,
8558c2ecf20Sopenharmony_ci	.buffer_bytes_max =	(2*128*1024),
8568c2ecf20Sopenharmony_ci	.period_bytes_min =	64,
8578c2ecf20Sopenharmony_ci	.period_bytes_max =	(128*1024),
8588c2ecf20Sopenharmony_ci	.periods_min =		2,
8598c2ecf20Sopenharmony_ci	.periods_max =		1024,
8608c2ecf20Sopenharmony_ci	.fifo_size =              0
8618c2ecf20Sopenharmony_ci};
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_cistatic int snd_usx2y_pcm_open(struct snd_pcm_substream *substream)
8668c2ecf20Sopenharmony_ci{
8678c2ecf20Sopenharmony_ci	struct snd_usx2y_substream	*subs = ((struct snd_usx2y_substream **)
8688c2ecf20Sopenharmony_ci					 snd_pcm_substream_chip(substream))[substream->stream];
8698c2ecf20Sopenharmony_ci	struct snd_pcm_runtime	*runtime = substream->runtime;
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci	if (subs->usx2y->chip_status & USX2Y_STAT_CHIP_MMAP_PCM_URBS)
8728c2ecf20Sopenharmony_ci		return -EBUSY;
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci	runtime->hw = snd_usx2y_2c;
8758c2ecf20Sopenharmony_ci	runtime->private_data = subs;
8768c2ecf20Sopenharmony_ci	subs->pcm_substream = substream;
8778c2ecf20Sopenharmony_ci	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, 1000, 200000);
8788c2ecf20Sopenharmony_ci	return 0;
8798c2ecf20Sopenharmony_ci}
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_cistatic int snd_usx2y_pcm_close(struct snd_pcm_substream *substream)
8848c2ecf20Sopenharmony_ci{
8858c2ecf20Sopenharmony_ci	struct snd_pcm_runtime *runtime = substream->runtime;
8868c2ecf20Sopenharmony_ci	struct snd_usx2y_substream *subs = runtime->private_data;
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci	subs->pcm_substream = NULL;
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci	return 0;
8918c2ecf20Sopenharmony_ci}
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_cistatic const struct snd_pcm_ops snd_usx2y_pcm_ops =
8958c2ecf20Sopenharmony_ci{
8968c2ecf20Sopenharmony_ci	.open =		snd_usx2y_pcm_open,
8978c2ecf20Sopenharmony_ci	.close =	snd_usx2y_pcm_close,
8988c2ecf20Sopenharmony_ci	.hw_params =	snd_usx2y_pcm_hw_params,
8998c2ecf20Sopenharmony_ci	.hw_free =	snd_usx2y_pcm_hw_free,
9008c2ecf20Sopenharmony_ci	.prepare =	snd_usx2y_pcm_prepare,
9018c2ecf20Sopenharmony_ci	.trigger =	snd_usx2y_pcm_trigger,
9028c2ecf20Sopenharmony_ci	.pointer =	snd_usx2y_pcm_pointer,
9038c2ecf20Sopenharmony_ci};
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci/*
9078c2ecf20Sopenharmony_ci * free a usb stream instance
9088c2ecf20Sopenharmony_ci */
9098c2ecf20Sopenharmony_cistatic void usx2y_audio_stream_free(struct snd_usx2y_substream **usx2y_substream)
9108c2ecf20Sopenharmony_ci{
9118c2ecf20Sopenharmony_ci	int stream;
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_ci	for_each_pcm_streams(stream) {
9148c2ecf20Sopenharmony_ci		kfree(usx2y_substream[stream]);
9158c2ecf20Sopenharmony_ci		usx2y_substream[stream] = NULL;
9168c2ecf20Sopenharmony_ci	}
9178c2ecf20Sopenharmony_ci}
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_cistatic void snd_usx2y_pcm_private_free(struct snd_pcm *pcm)
9208c2ecf20Sopenharmony_ci{
9218c2ecf20Sopenharmony_ci	struct snd_usx2y_substream **usx2y_stream = pcm->private_data;
9228c2ecf20Sopenharmony_ci	if (usx2y_stream)
9238c2ecf20Sopenharmony_ci		usx2y_audio_stream_free(usx2y_stream);
9248c2ecf20Sopenharmony_ci}
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_cistatic int usx2y_audio_stream_new(struct snd_card *card, int playback_endpoint, int capture_endpoint)
9278c2ecf20Sopenharmony_ci{
9288c2ecf20Sopenharmony_ci	struct snd_pcm *pcm;
9298c2ecf20Sopenharmony_ci	int err, i;
9308c2ecf20Sopenharmony_ci	struct snd_usx2y_substream **usx2y_substream =
9318c2ecf20Sopenharmony_ci		usx2y(card)->subs + 2 * usx2y(card)->pcm_devs;
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_ci	for (i = playback_endpoint ? SNDRV_PCM_STREAM_PLAYBACK : SNDRV_PCM_STREAM_CAPTURE;
9348c2ecf20Sopenharmony_ci	     i <= SNDRV_PCM_STREAM_CAPTURE; ++i) {
9358c2ecf20Sopenharmony_ci		usx2y_substream[i] = kzalloc(sizeof(struct snd_usx2y_substream), GFP_KERNEL);
9368c2ecf20Sopenharmony_ci		if (!usx2y_substream[i])
9378c2ecf20Sopenharmony_ci			return -ENOMEM;
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_ci		usx2y_substream[i]->usx2y = usx2y(card);
9408c2ecf20Sopenharmony_ci	}
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci	if (playback_endpoint)
9438c2ecf20Sopenharmony_ci		usx2y_substream[SNDRV_PCM_STREAM_PLAYBACK]->endpoint = playback_endpoint;
9448c2ecf20Sopenharmony_ci	usx2y_substream[SNDRV_PCM_STREAM_CAPTURE]->endpoint = capture_endpoint;
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci	err = snd_pcm_new(card, NAME_ALLCAPS" Audio", usx2y(card)->pcm_devs,
9478c2ecf20Sopenharmony_ci			  playback_endpoint ? 1 : 0, 1,
9488c2ecf20Sopenharmony_ci			  &pcm);
9498c2ecf20Sopenharmony_ci	if (err < 0) {
9508c2ecf20Sopenharmony_ci		usx2y_audio_stream_free(usx2y_substream);
9518c2ecf20Sopenharmony_ci		return err;
9528c2ecf20Sopenharmony_ci	}
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_ci	if (playback_endpoint)
9558c2ecf20Sopenharmony_ci		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_usx2y_pcm_ops);
9568c2ecf20Sopenharmony_ci	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_usx2y_pcm_ops);
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci	pcm->private_data = usx2y_substream;
9598c2ecf20Sopenharmony_ci	pcm->private_free = snd_usx2y_pcm_private_free;
9608c2ecf20Sopenharmony_ci	pcm->info_flags = 0;
9618c2ecf20Sopenharmony_ci
9628c2ecf20Sopenharmony_ci	sprintf(pcm->name, NAME_ALLCAPS" Audio #%d", usx2y(card)->pcm_devs);
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	if (playback_endpoint) {
9658c2ecf20Sopenharmony_ci		snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream,
9668c2ecf20Sopenharmony_ci					   SNDRV_DMA_TYPE_CONTINUOUS,
9678c2ecf20Sopenharmony_ci					   NULL,
9688c2ecf20Sopenharmony_ci					   64*1024, 128*1024);
9698c2ecf20Sopenharmony_ci	}
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci	snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
9728c2ecf20Sopenharmony_ci				   SNDRV_DMA_TYPE_CONTINUOUS,
9738c2ecf20Sopenharmony_ci				   NULL,
9748c2ecf20Sopenharmony_ci				   64*1024, 128*1024);
9758c2ecf20Sopenharmony_ci	usx2y(card)->pcm_devs++;
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_ci	return 0;
9788c2ecf20Sopenharmony_ci}
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_ci/*
9818c2ecf20Sopenharmony_ci * create a chip instance and set its names.
9828c2ecf20Sopenharmony_ci */
9838c2ecf20Sopenharmony_ciint usx2y_audio_create(struct snd_card *card)
9848c2ecf20Sopenharmony_ci{
9858c2ecf20Sopenharmony_ci	int err = 0;
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&usx2y(card)->pcm_list);
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_ci	if (0 > (err = usx2y_audio_stream_new(card, 0xA, 0x8)))
9908c2ecf20Sopenharmony_ci		return err;
9918c2ecf20Sopenharmony_ci	if (le16_to_cpu(usx2y(card)->dev->descriptor.idProduct) == USB_ID_US428)
9928c2ecf20Sopenharmony_ci	     if (0 > (err = usx2y_audio_stream_new(card, 0, 0xA)))
9938c2ecf20Sopenharmony_ci		     return err;
9948c2ecf20Sopenharmony_ci	if (le16_to_cpu(usx2y(card)->dev->descriptor.idProduct) != USB_ID_US122)
9958c2ecf20Sopenharmony_ci		err = usx2y_rate_set(usx2y(card), 44100);	// Lets us428 recognize output-volume settings, disturbs us122.
9968c2ecf20Sopenharmony_ci	return err;
9978c2ecf20Sopenharmony_ci}
998