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)  * Linux driver for TerraTec DMX 6Fire USB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * PCM driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author:	Torsten Schenk <torsten.schenk@zoho.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Created:	Jan 01, 2011
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * Copyright:	(C) Torsten Schenk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include "pcm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "chip.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "comm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include "control.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 	OUT_N_CHANNELS = 6, IN_N_CHANNELS = 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) /* keep next two synced with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * FW_EP_W_MAX_PACKET_SIZE[] and RATES_MAX_PACKET_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * and CONTROL_RATE_XXX in control.h */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static const int rates_in_packet_size[] = { 228, 228, 420, 420, 404, 404 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static const int rates_out_packet_size[] = { 228, 228, 420, 420, 604, 604 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static const int rates[] = { 44100, 48000, 88200, 96000, 176400, 192000 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) static const int rates_alsaid[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	SNDRV_PCM_RATE_44100, SNDRV_PCM_RATE_48000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	SNDRV_PCM_RATE_88200, SNDRV_PCM_RATE_96000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	SNDRV_PCM_RATE_176400, SNDRV_PCM_RATE_192000 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) enum { /* settings for pcm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	OUT_EP = 6, IN_EP = 2, MAX_BUFSIZE = 128 * 1024
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) enum { /* pcm streaming states */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	STREAM_DISABLED, /* no pcm streaming */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	STREAM_STARTING, /* pcm streaming requested, waiting to become ready */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	STREAM_RUNNING, /* pcm streaming running */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	STREAM_STOPPING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static const struct snd_pcm_hardware pcm_hw = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	.info = SNDRV_PCM_INFO_MMAP |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		SNDRV_PCM_INFO_INTERLEAVED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		SNDRV_PCM_INFO_BLOCK_TRANSFER |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		SNDRV_PCM_INFO_MMAP_VALID |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		SNDRV_PCM_INFO_BATCH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	.formats = SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	.rates = SNDRV_PCM_RATE_44100 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		SNDRV_PCM_RATE_48000 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		SNDRV_PCM_RATE_88200 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		SNDRV_PCM_RATE_96000 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		SNDRV_PCM_RATE_176400 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		SNDRV_PCM_RATE_192000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	.rate_min = 44100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	.rate_max = 192000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	.channels_min = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	.channels_max = 0, /* set in pcm_open, depending on capture/playback */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	.buffer_bytes_max = MAX_BUFSIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	.period_bytes_min = PCM_N_PACKETS_PER_URB * (PCM_MAX_PACKET_SIZE - 4),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	.period_bytes_max = MAX_BUFSIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	.periods_min = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	.periods_max = 1024
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) static int usb6fire_pcm_set_rate(struct pcm_runtime *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	struct control_runtime *ctrl_rt = rt->chip->control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	ctrl_rt->usb_streaming = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	ret = ctrl_rt->update_streaming(ctrl_rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		dev_err(&rt->chip->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			"error stopping streaming while setting samplerate %d.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			rates[rt->rate]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		return ret;
^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) 	ret = ctrl_rt->set_rate(ctrl_rt, rt->rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		dev_err(&rt->chip->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			"error setting samplerate %d.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			rates[rt->rate]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	ret = ctrl_rt->set_channels(ctrl_rt, OUT_N_CHANNELS, IN_N_CHANNELS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			false, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		dev_err(&rt->chip->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			"error initializing channels while setting samplerate %d.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			rates[rt->rate]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	ctrl_rt->usb_streaming = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	ret = ctrl_rt->update_streaming(ctrl_rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		dev_err(&rt->chip->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			"error starting streaming while setting samplerate %d.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			rates[rt->rate]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	rt->in_n_analog = IN_N_CHANNELS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	rt->out_n_analog = OUT_N_CHANNELS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	rt->in_packet_size = rates_in_packet_size[rt->rate];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	rt->out_packet_size = rates_out_packet_size[rt->rate];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static struct pcm_substream *usb6fire_pcm_get_substream(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		struct snd_pcm_substream *alsa_sub)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		return &rt->playback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	else if (alsa_sub->stream == SNDRV_PCM_STREAM_CAPTURE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		return &rt->capture;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	dev_err(&rt->chip->dev->dev, "error getting pcm substream slot.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /* call with stream_mutex locked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static void usb6fire_pcm_stream_stop(struct pcm_runtime *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	struct control_runtime *ctrl_rt = rt->chip->control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (rt->stream_state != STREAM_DISABLED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		rt->stream_state = STREAM_STOPPING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		for (i = 0; i < PCM_N_URBS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			usb_kill_urb(&rt->in_urbs[i].instance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			usb_kill_urb(&rt->out_urbs[i].instance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		ctrl_rt->usb_streaming = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		ctrl_rt->update_streaming(ctrl_rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		rt->stream_state = STREAM_DISABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /* call with stream_mutex locked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static int usb6fire_pcm_stream_start(struct pcm_runtime *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	int k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	struct usb_iso_packet_descriptor *packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (rt->stream_state == STREAM_DISABLED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		/* submit our in urbs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		rt->stream_wait_cond = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		rt->stream_state = STREAM_STARTING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		for (i = 0; i < PCM_N_URBS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			for (k = 0; k < PCM_N_PACKETS_PER_URB; k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 				packet = &rt->in_urbs[i].packets[k];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 				packet->offset = k * rt->in_packet_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 				packet->length = rt->in_packet_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 				packet->actual_length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 				packet->status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			ret = usb_submit_urb(&rt->in_urbs[i].instance,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 					GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 				usb6fire_pcm_stream_stop(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		/* wait for first out urb to return (sent in in urb handler) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		wait_event_timeout(rt->stream_wait_queue, rt->stream_wait_cond,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 				HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		if (rt->stream_wait_cond)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			rt->stream_state = STREAM_RUNNING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 			usb6fire_pcm_stream_stop(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /* call with substream locked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static void usb6fire_pcm_capture(struct pcm_substream *sub, struct pcm_urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	int frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	int frame_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	unsigned int total_length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	struct pcm_runtime *rt = snd_pcm_substream_chip(sub->instance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	struct snd_pcm_runtime *alsa_rt = sub->instance->runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	u32 *src = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	u32 *dest = (u32 *) (alsa_rt->dma_area + sub->dma_off
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			* (alsa_rt->frame_bits >> 3));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	u32 *dest_end = (u32 *) (alsa_rt->dma_area + alsa_rt->buffer_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			* (alsa_rt->frame_bits >> 3));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	int bytes_per_frame = alsa_rt->channels << 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	for (i = 0; i < PCM_N_PACKETS_PER_URB; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		/* at least 4 header bytes for valid packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		 * after that: 32 bits per sample for analog channels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		if (urb->packets[i].actual_length > 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			frame_count = (urb->packets[i].actual_length - 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 					/ (rt->in_n_analog << 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			frame_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		if (alsa_rt->format == SNDRV_PCM_FORMAT_S24_LE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			src = (u32 *) (urb->buffer + total_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		else if (alsa_rt->format == SNDRV_PCM_FORMAT_S32_LE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			src = (u32 *) (urb->buffer - 1 + total_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		src++; /* skip leading 4 bytes of every packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		total_length += urb->packets[i].length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		for (frame = 0; frame < frame_count; frame++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 			memcpy(dest, src, bytes_per_frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 			dest += alsa_rt->channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 			src += rt->in_n_analog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			sub->dma_off++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 			sub->period_off++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 			if (dest == dest_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 				sub->dma_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 				dest = (u32 *) alsa_rt->dma_area;
^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) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) /* call with substream locked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) static void usb6fire_pcm_playback(struct pcm_substream *sub,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		struct pcm_urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	int frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	int frame_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	struct pcm_runtime *rt = snd_pcm_substream_chip(sub->instance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	struct snd_pcm_runtime *alsa_rt = sub->instance->runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	u32 *src = (u32 *) (alsa_rt->dma_area + sub->dma_off
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			* (alsa_rt->frame_bits >> 3));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	u32 *src_end = (u32 *) (alsa_rt->dma_area + alsa_rt->buffer_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			* (alsa_rt->frame_bits >> 3));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	u32 *dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	int bytes_per_frame = alsa_rt->channels << 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	if (alsa_rt->format == SNDRV_PCM_FORMAT_S32_LE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		dest = (u32 *) (urb->buffer - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	else if (alsa_rt->format == SNDRV_PCM_FORMAT_S24_LE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		dest = (u32 *) (urb->buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		dev_err(&rt->chip->dev->dev, "Unknown sample format.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	for (i = 0; i < PCM_N_PACKETS_PER_URB; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		/* at least 4 header bytes for valid packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		 * after that: 32 bits per sample for analog channels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		if (urb->packets[i].length > 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			frame_count = (urb->packets[i].length - 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 					/ (rt->out_n_analog << 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 			frame_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		dest++; /* skip leading 4 bytes of every frame */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		for (frame = 0; frame < frame_count; frame++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 			memcpy(dest, src, bytes_per_frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			src += alsa_rt->channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			dest += rt->out_n_analog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 			sub->dma_off++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			sub->period_off++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			if (src == src_end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 				src = (u32 *) alsa_rt->dma_area;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 				sub->dma_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static void usb6fire_pcm_in_urb_handler(struct urb *usb_urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	struct pcm_urb *in_urb = usb_urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	struct pcm_urb *out_urb = in_urb->peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	struct pcm_runtime *rt = in_urb->chip->pcm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	struct pcm_substream *sub;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	int total_length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	int frame_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	int frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	int channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	u8 *dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	if (usb_urb->status || rt->panic || rt->stream_state == STREAM_STOPPING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	for (i = 0; i < PCM_N_PACKETS_PER_URB; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		if (in_urb->packets[i].status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 			rt->panic = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	if (rt->stream_state == STREAM_DISABLED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		dev_err(&rt->chip->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			"internal error: stream disabled in in-urb handler.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	/* receive our capture data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	sub = &rt->capture;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	spin_lock_irqsave(&sub->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	if (sub->active) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		usb6fire_pcm_capture(sub, in_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		if (sub->period_off >= sub->instance->runtime->period_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			sub->period_off %= sub->instance->runtime->period_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 			spin_unlock_irqrestore(&sub->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			snd_pcm_period_elapsed(sub->instance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			spin_unlock_irqrestore(&sub->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		spin_unlock_irqrestore(&sub->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	/* setup out urb structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	for (i = 0; i < PCM_N_PACKETS_PER_URB; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		out_urb->packets[i].offset = total_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		out_urb->packets[i].length = (in_urb->packets[i].actual_length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 				- 4) / (rt->in_n_analog << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 				* (rt->out_n_analog << 2) + 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		out_urb->packets[i].status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		total_length += out_urb->packets[i].length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	memset(out_urb->buffer, 0, total_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	/* now send our playback data (if a free out urb was found) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	sub = &rt->playback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	spin_lock_irqsave(&sub->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	if (sub->active) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		usb6fire_pcm_playback(sub, out_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		if (sub->period_off >= sub->instance->runtime->period_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 			sub->period_off %= sub->instance->runtime->period_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			spin_unlock_irqrestore(&sub->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			snd_pcm_period_elapsed(sub->instance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 			spin_unlock_irqrestore(&sub->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		spin_unlock_irqrestore(&sub->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	/* setup the 4th byte of each sample (0x40 for analog channels) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	dest = out_urb->buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	for (i = 0; i < PCM_N_PACKETS_PER_URB; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		if (out_urb->packets[i].length >= 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 			frame_count = (out_urb->packets[i].length - 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 					/ (rt->out_n_analog << 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 			*(dest++) = 0xaa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 			*(dest++) = 0xaa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 			*(dest++) = frame_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 			*(dest++) = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			for (frame = 0; frame < frame_count; frame++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 				for (channel = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 						channel < rt->out_n_analog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 						channel++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 					dest += 3; /* skip sample data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 					*(dest++) = 0x40;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	usb_submit_urb(&out_urb->instance, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	usb_submit_urb(&in_urb->instance, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) static void usb6fire_pcm_out_urb_handler(struct urb *usb_urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	struct pcm_urb *urb = usb_urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	struct pcm_runtime *rt = urb->chip->pcm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	if (rt->stream_state == STREAM_STARTING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		rt->stream_wait_cond = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		wake_up(&rt->stream_wait_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) static int usb6fire_pcm_open(struct snd_pcm_substream *alsa_sub)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	struct pcm_substream *sub = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	if (rt->panic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		return -EPIPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	mutex_lock(&rt->stream_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	alsa_rt->hw = pcm_hw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	if (alsa_sub->stream == SNDRV_PCM_STREAM_PLAYBACK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		if (rt->rate < ARRAY_SIZE(rates))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 			alsa_rt->hw.rates = rates_alsaid[rt->rate];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		alsa_rt->hw.channels_max = OUT_N_CHANNELS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		sub = &rt->playback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	} else if (alsa_sub->stream == SNDRV_PCM_STREAM_CAPTURE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		if (rt->rate < ARRAY_SIZE(rates))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 			alsa_rt->hw.rates = rates_alsaid[rt->rate];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		alsa_rt->hw.channels_max = IN_N_CHANNELS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		sub = &rt->capture;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	if (!sub) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		mutex_unlock(&rt->stream_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		dev_err(&rt->chip->dev->dev, "invalid stream type.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	sub->instance = alsa_sub;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	sub->active = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	mutex_unlock(&rt->stream_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) static int usb6fire_pcm_close(struct snd_pcm_substream *alsa_sub)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	if (rt->panic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	mutex_lock(&rt->stream_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	if (sub) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		/* deactivate substream */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		spin_lock_irqsave(&sub->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		sub->instance = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		sub->active = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		spin_unlock_irqrestore(&sub->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		/* all substreams closed? if so, stop streaming */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		if (!rt->playback.instance && !rt->capture.instance) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 			usb6fire_pcm_stream_stop(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 			rt->rate = ARRAY_SIZE(rates);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	mutex_unlock(&rt->stream_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) static int usb6fire_pcm_prepare(struct snd_pcm_substream *alsa_sub)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	struct snd_pcm_runtime *alsa_rt = alsa_sub->runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	if (rt->panic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		return -EPIPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	if (!sub)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	mutex_lock(&rt->stream_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	sub->dma_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	sub->period_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	if (rt->stream_state == STREAM_DISABLED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		for (rt->rate = 0; rt->rate < ARRAY_SIZE(rates); rt->rate++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 			if (alsa_rt->rate == rates[rt->rate])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		if (rt->rate == ARRAY_SIZE(rates)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 			mutex_unlock(&rt->stream_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 			dev_err(&rt->chip->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 				"invalid rate %d in prepare.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 				alsa_rt->rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		ret = usb6fire_pcm_set_rate(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 			mutex_unlock(&rt->stream_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		ret = usb6fire_pcm_stream_start(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 			mutex_unlock(&rt->stream_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 			dev_err(&rt->chip->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 				"could not start pcm stream.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	mutex_unlock(&rt->stream_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) static int usb6fire_pcm_trigger(struct snd_pcm_substream *alsa_sub, int cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	if (rt->panic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		return -EPIPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	if (!sub)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	case SNDRV_PCM_TRIGGER_START:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		spin_lock_irqsave(&sub->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		sub->active = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		spin_unlock_irqrestore(&sub->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	case SNDRV_PCM_TRIGGER_STOP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		spin_lock_irqsave(&sub->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		sub->active = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		spin_unlock_irqrestore(&sub->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	}
^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) static snd_pcm_uframes_t usb6fire_pcm_pointer(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		struct snd_pcm_substream *alsa_sub)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	struct pcm_substream *sub = usb6fire_pcm_get_substream(alsa_sub);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	struct pcm_runtime *rt = snd_pcm_substream_chip(alsa_sub);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	snd_pcm_uframes_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	if (rt->panic || !sub)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		return SNDRV_PCM_POS_XRUN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	spin_lock_irqsave(&sub->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	ret = sub->dma_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	spin_unlock_irqrestore(&sub->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	return ret;
^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) static const struct snd_pcm_ops pcm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	.open = usb6fire_pcm_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	.close = usb6fire_pcm_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	.prepare = usb6fire_pcm_prepare,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	.trigger = usb6fire_pcm_trigger,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	.pointer = usb6fire_pcm_pointer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) static void usb6fire_pcm_init_urb(struct pcm_urb *urb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 				  struct sfire_chip *chip, bool in, int ep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 				  void (*handler)(struct urb *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	urb->chip = chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	usb_init_urb(&urb->instance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	urb->instance.transfer_buffer = urb->buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	urb->instance.transfer_buffer_length =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 			PCM_N_PACKETS_PER_URB * PCM_MAX_PACKET_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	urb->instance.dev = chip->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	urb->instance.pipe = in ? usb_rcvisocpipe(chip->dev, ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 			: usb_sndisocpipe(chip->dev, ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	urb->instance.interval = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	urb->instance.complete = handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	urb->instance.context = urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	urb->instance.number_of_packets = PCM_N_PACKETS_PER_URB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) static int usb6fire_pcm_buffers_init(struct pcm_runtime *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	for (i = 0; i < PCM_N_URBS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 		rt->out_urbs[i].buffer = kcalloc(PCM_MAX_PACKET_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 						 PCM_N_PACKETS_PER_URB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 						 GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 		if (!rt->out_urbs[i].buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 		rt->in_urbs[i].buffer = kcalloc(PCM_MAX_PACKET_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 						PCM_N_PACKETS_PER_URB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 						GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 		if (!rt->in_urbs[i].buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) static void usb6fire_pcm_buffers_destroy(struct pcm_runtime *rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	for (i = 0; i < PCM_N_URBS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 		kfree(rt->out_urbs[i].buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 		kfree(rt->in_urbs[i].buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) int usb6fire_pcm_init(struct sfire_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	struct snd_pcm *pcm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	struct pcm_runtime *rt =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 			kzalloc(sizeof(struct pcm_runtime), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	if (!rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	ret = usb6fire_pcm_buffers_init(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 		usb6fire_pcm_buffers_destroy(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 		kfree(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	rt->chip = chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	rt->stream_state = STREAM_DISABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	rt->rate = ARRAY_SIZE(rates);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	init_waitqueue_head(&rt->stream_wait_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	mutex_init(&rt->stream_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	spin_lock_init(&rt->playback.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	spin_lock_init(&rt->capture.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	for (i = 0; i < PCM_N_URBS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 		usb6fire_pcm_init_urb(&rt->in_urbs[i], chip, true, IN_EP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 				usb6fire_pcm_in_urb_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 		usb6fire_pcm_init_urb(&rt->out_urbs[i], chip, false, OUT_EP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 				usb6fire_pcm_out_urb_handler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 		rt->in_urbs[i].peer = &rt->out_urbs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 		rt->out_urbs[i].peer = &rt->in_urbs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	ret = snd_pcm_new(chip->card, "DMX6FireUSB", 0, 1, 1, &pcm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 		usb6fire_pcm_buffers_destroy(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 		kfree(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 		dev_err(&chip->dev->dev, "cannot create pcm instance.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	pcm->private_data = rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	strcpy(pcm->name, "DMX 6Fire USB");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &pcm_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &pcm_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	rt->instance = pcm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	chip->pcm = rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) void usb6fire_pcm_abort(struct sfire_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	struct pcm_runtime *rt = chip->pcm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 	if (rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 		rt->panic = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 		if (rt->playback.instance)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 			snd_pcm_stop_xrun(rt->playback.instance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 		if (rt->capture.instance)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 			snd_pcm_stop_xrun(rt->capture.instance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 		for (i = 0; i < PCM_N_URBS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 			usb_poison_urb(&rt->in_urbs[i].instance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 			usb_poison_urb(&rt->out_urbs[i].instance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) void usb6fire_pcm_destroy(struct sfire_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	struct pcm_runtime *rt = chip->pcm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	usb6fire_pcm_buffers_destroy(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	kfree(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	chip->pcm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) }