xref: /linux/sound/firewire/amdtp-stream.c (revision b164d2fd6e4985e9270755477dde063e6e48461e)
1d67c46b9STakashi Sakamoto /*
2d67c46b9STakashi Sakamoto  * Audio and Music Data Transmission Protocol (IEC 61883-6) streams
3d67c46b9STakashi Sakamoto  * with Common Isochronous Packet (IEC 61883-1) headers
4d67c46b9STakashi Sakamoto  *
5d67c46b9STakashi Sakamoto  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
6d67c46b9STakashi Sakamoto  * Licensed under the terms of the GNU General Public License, version 2.
7d67c46b9STakashi Sakamoto  */
8d67c46b9STakashi Sakamoto 
9d67c46b9STakashi Sakamoto #include <linux/device.h>
10d67c46b9STakashi Sakamoto #include <linux/err.h>
11d67c46b9STakashi Sakamoto #include <linux/firewire.h>
12d67c46b9STakashi Sakamoto #include <linux/module.h>
13d67c46b9STakashi Sakamoto #include <linux/slab.h>
14d67c46b9STakashi Sakamoto #include <sound/pcm.h>
15d67c46b9STakashi Sakamoto #include <sound/pcm_params.h>
16d67c46b9STakashi Sakamoto #include "amdtp-stream.h"
17d67c46b9STakashi Sakamoto 
18d67c46b9STakashi Sakamoto #define TICKS_PER_CYCLE		3072
19d67c46b9STakashi Sakamoto #define CYCLES_PER_SECOND	8000
20d67c46b9STakashi Sakamoto #define TICKS_PER_SECOND	(TICKS_PER_CYCLE * CYCLES_PER_SECOND)
21d67c46b9STakashi Sakamoto 
220c95c1d6STakashi Sakamoto /* Always support Linux tracing subsystem. */
230c95c1d6STakashi Sakamoto #define CREATE_TRACE_POINTS
240c95c1d6STakashi Sakamoto #include "amdtp-stream-trace.h"
250c95c1d6STakashi Sakamoto 
26d67c46b9STakashi Sakamoto #define TRANSFER_DELAY_TICKS	0x2e00 /* 479.17 microseconds */
27d67c46b9STakashi Sakamoto 
28d67c46b9STakashi Sakamoto /* isochronous header parameters */
29d67c46b9STakashi Sakamoto #define ISO_DATA_LENGTH_SHIFT	16
303b196c39STakashi Sakamoto #define TAG_NO_CIP_HEADER	0
31d67c46b9STakashi Sakamoto #define TAG_CIP			1
32d67c46b9STakashi Sakamoto 
33d67c46b9STakashi Sakamoto /* common isochronous packet header parameters */
34d67c46b9STakashi Sakamoto #define CIP_EOH_SHIFT		31
35d67c46b9STakashi Sakamoto #define CIP_EOH			(1u << CIP_EOH_SHIFT)
36d67c46b9STakashi Sakamoto #define CIP_EOH_MASK		0x80000000
37d67c46b9STakashi Sakamoto #define CIP_SID_SHIFT		24
38d67c46b9STakashi Sakamoto #define CIP_SID_MASK		0x3f000000
39d67c46b9STakashi Sakamoto #define CIP_DBS_MASK		0x00ff0000
40d67c46b9STakashi Sakamoto #define CIP_DBS_SHIFT		16
419863874fSTakashi Sakamoto #define CIP_SPH_MASK		0x00000400
429863874fSTakashi Sakamoto #define CIP_SPH_SHIFT		10
43d67c46b9STakashi Sakamoto #define CIP_DBC_MASK		0x000000ff
44d67c46b9STakashi Sakamoto #define CIP_FMT_SHIFT		24
45d67c46b9STakashi Sakamoto #define CIP_FMT_MASK		0x3f000000
46d67c46b9STakashi Sakamoto #define CIP_FDF_MASK		0x00ff0000
47d67c46b9STakashi Sakamoto #define CIP_FDF_SHIFT		16
48d67c46b9STakashi Sakamoto #define CIP_SYT_MASK		0x0000ffff
49d67c46b9STakashi Sakamoto #define CIP_SYT_NO_INFO		0xffff
50d67c46b9STakashi Sakamoto 
5151c29fd2STakashi Sakamoto /* Audio and Music transfer protocol specific parameters */
52d67c46b9STakashi Sakamoto #define CIP_FMT_AM		0x10
53d67c46b9STakashi Sakamoto #define AMDTP_FDF_NO_DATA	0xff
54d67c46b9STakashi Sakamoto 
55d67c46b9STakashi Sakamoto /* TODO: make these configurable */
56d67c46b9STakashi Sakamoto #define INTERRUPT_INTERVAL	16
57d67c46b9STakashi Sakamoto #define QUEUE_LENGTH		48
58d67c46b9STakashi Sakamoto 
59d67c46b9STakashi Sakamoto #define IN_PACKET_HEADER_SIZE	4
60d67c46b9STakashi Sakamoto #define OUT_PACKET_HEADER_SIZE	0
61d67c46b9STakashi Sakamoto 
62d67c46b9STakashi Sakamoto static void pcm_period_tasklet(unsigned long data);
63d67c46b9STakashi Sakamoto 
64d67c46b9STakashi Sakamoto /**
65d67c46b9STakashi Sakamoto  * amdtp_stream_init - initialize an AMDTP stream structure
66d67c46b9STakashi Sakamoto  * @s: the AMDTP stream to initialize
67d67c46b9STakashi Sakamoto  * @unit: the target of the stream
68d67c46b9STakashi Sakamoto  * @dir: the direction of stream
69d67c46b9STakashi Sakamoto  * @flags: the packet transmission method to use
705955815eSTakashi Sakamoto  * @fmt: the value of fmt field in CIP header
71df075feeSTakashi Sakamoto  * @process_data_blocks: callback handler to process data blocks
72df075feeSTakashi Sakamoto  * @protocol_size: the size to allocate newly for protocol
73d67c46b9STakashi Sakamoto  */
74d67c46b9STakashi Sakamoto int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
755955815eSTakashi Sakamoto 		      enum amdtp_stream_direction dir, enum cip_flags flags,
76df075feeSTakashi Sakamoto 		      unsigned int fmt,
77df075feeSTakashi Sakamoto 		      amdtp_stream_process_data_blocks_t process_data_blocks,
78df075feeSTakashi Sakamoto 		      unsigned int protocol_size)
79d67c46b9STakashi Sakamoto {
80df075feeSTakashi Sakamoto 	if (process_data_blocks == NULL)
81df075feeSTakashi Sakamoto 		return -EINVAL;
82df075feeSTakashi Sakamoto 
83df075feeSTakashi Sakamoto 	s->protocol = kzalloc(protocol_size, GFP_KERNEL);
84df075feeSTakashi Sakamoto 	if (!s->protocol)
85df075feeSTakashi Sakamoto 		return -ENOMEM;
86df075feeSTakashi Sakamoto 
87d67c46b9STakashi Sakamoto 	s->unit = unit;
88d67c46b9STakashi Sakamoto 	s->direction = dir;
89d67c46b9STakashi Sakamoto 	s->flags = flags;
90d67c46b9STakashi Sakamoto 	s->context = ERR_PTR(-1);
91d67c46b9STakashi Sakamoto 	mutex_init(&s->mutex);
92d67c46b9STakashi Sakamoto 	tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
93d67c46b9STakashi Sakamoto 	s->packet_index = 0;
94d67c46b9STakashi Sakamoto 
95d67c46b9STakashi Sakamoto 	init_waitqueue_head(&s->callback_wait);
96d67c46b9STakashi Sakamoto 	s->callbacked = false;
97d67c46b9STakashi Sakamoto 
985955815eSTakashi Sakamoto 	s->fmt = fmt;
99df075feeSTakashi Sakamoto 	s->process_data_blocks = process_data_blocks;
100d67c46b9STakashi Sakamoto 
101d67c46b9STakashi Sakamoto 	return 0;
102d67c46b9STakashi Sakamoto }
103d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_init);
104d67c46b9STakashi Sakamoto 
105d67c46b9STakashi Sakamoto /**
106d67c46b9STakashi Sakamoto  * amdtp_stream_destroy - free stream resources
107d67c46b9STakashi Sakamoto  * @s: the AMDTP stream to destroy
108d67c46b9STakashi Sakamoto  */
109d67c46b9STakashi Sakamoto void amdtp_stream_destroy(struct amdtp_stream *s)
110d67c46b9STakashi Sakamoto {
11144c376b9STakashi Sakamoto 	/* Not initialized. */
11244c376b9STakashi Sakamoto 	if (s->protocol == NULL)
11344c376b9STakashi Sakamoto 		return;
11444c376b9STakashi Sakamoto 
115d67c46b9STakashi Sakamoto 	WARN_ON(amdtp_stream_running(s));
116df075feeSTakashi Sakamoto 	kfree(s->protocol);
117d67c46b9STakashi Sakamoto 	mutex_destroy(&s->mutex);
118d67c46b9STakashi Sakamoto }
119d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_destroy);
120d67c46b9STakashi Sakamoto 
121d67c46b9STakashi Sakamoto const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
122d67c46b9STakashi Sakamoto 	[CIP_SFC_32000]  =  8,
123d67c46b9STakashi Sakamoto 	[CIP_SFC_44100]  =  8,
124d67c46b9STakashi Sakamoto 	[CIP_SFC_48000]  =  8,
125d67c46b9STakashi Sakamoto 	[CIP_SFC_88200]  = 16,
126d67c46b9STakashi Sakamoto 	[CIP_SFC_96000]  = 16,
127d67c46b9STakashi Sakamoto 	[CIP_SFC_176400] = 32,
128d67c46b9STakashi Sakamoto 	[CIP_SFC_192000] = 32,
129d67c46b9STakashi Sakamoto };
130d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_syt_intervals);
131d67c46b9STakashi Sakamoto 
132d67c46b9STakashi Sakamoto const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
133d67c46b9STakashi Sakamoto 	[CIP_SFC_32000]  =  32000,
134d67c46b9STakashi Sakamoto 	[CIP_SFC_44100]  =  44100,
135d67c46b9STakashi Sakamoto 	[CIP_SFC_48000]  =  48000,
136d67c46b9STakashi Sakamoto 	[CIP_SFC_88200]  =  88200,
137d67c46b9STakashi Sakamoto 	[CIP_SFC_96000]  =  96000,
138d67c46b9STakashi Sakamoto 	[CIP_SFC_176400] = 176400,
139d67c46b9STakashi Sakamoto 	[CIP_SFC_192000] = 192000,
140d67c46b9STakashi Sakamoto };
141d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_rate_table);
142d67c46b9STakashi Sakamoto 
143d67c46b9STakashi Sakamoto /**
144d67c46b9STakashi Sakamoto  * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
145d67c46b9STakashi Sakamoto  * @s:		the AMDTP stream, which must be initialized.
146d67c46b9STakashi Sakamoto  * @runtime:	the PCM substream runtime
147d67c46b9STakashi Sakamoto  */
148d67c46b9STakashi Sakamoto int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
149d67c46b9STakashi Sakamoto 					struct snd_pcm_runtime *runtime)
150d67c46b9STakashi Sakamoto {
151d67c46b9STakashi Sakamoto 	int err;
152d67c46b9STakashi Sakamoto 
153d67c46b9STakashi Sakamoto 	/*
154d67c46b9STakashi Sakamoto 	 * Currently firewire-lib processes 16 packets in one software
155d67c46b9STakashi Sakamoto 	 * interrupt callback. This equals to 2msec but actually the
156d67c46b9STakashi Sakamoto 	 * interval of the interrupts has a jitter.
157d67c46b9STakashi Sakamoto 	 * Additionally, even if adding a constraint to fit period size to
158d67c46b9STakashi Sakamoto 	 * 2msec, actual calculated frames per period doesn't equal to 2msec,
159d67c46b9STakashi Sakamoto 	 * depending on sampling rate.
160d67c46b9STakashi Sakamoto 	 * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec.
161d67c46b9STakashi Sakamoto 	 * Here let us use 5msec for safe period interrupt.
162d67c46b9STakashi Sakamoto 	 */
163d67c46b9STakashi Sakamoto 	err = snd_pcm_hw_constraint_minmax(runtime,
164d67c46b9STakashi Sakamoto 					   SNDRV_PCM_HW_PARAM_PERIOD_TIME,
165d67c46b9STakashi Sakamoto 					   5000, UINT_MAX);
166d67c46b9STakashi Sakamoto 	if (err < 0)
167d67c46b9STakashi Sakamoto 		goto end;
168d67c46b9STakashi Sakamoto 
169d67c46b9STakashi Sakamoto 	/* Non-Blocking stream has no more constraints */
170d67c46b9STakashi Sakamoto 	if (!(s->flags & CIP_BLOCKING))
171d67c46b9STakashi Sakamoto 		goto end;
172d67c46b9STakashi Sakamoto 
173d67c46b9STakashi Sakamoto 	/*
174d67c46b9STakashi Sakamoto 	 * One AMDTP packet can include some frames. In blocking mode, the
175d67c46b9STakashi Sakamoto 	 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
176d67c46b9STakashi Sakamoto 	 * depending on its sampling rate. For accurate period interrupt, it's
177d67c46b9STakashi Sakamoto 	 * preferrable to align period/buffer sizes to current SYT_INTERVAL.
178d67c46b9STakashi Sakamoto 	 *
179d67c46b9STakashi Sakamoto 	 * TODO: These constraints can be improved with proper rules.
180d67c46b9STakashi Sakamoto 	 * Currently apply LCM of SYT_INTERVALs.
181d67c46b9STakashi Sakamoto 	 */
182d67c46b9STakashi Sakamoto 	err = snd_pcm_hw_constraint_step(runtime, 0,
183d67c46b9STakashi Sakamoto 					 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
184d67c46b9STakashi Sakamoto 	if (err < 0)
185d67c46b9STakashi Sakamoto 		goto end;
186d67c46b9STakashi Sakamoto 	err = snd_pcm_hw_constraint_step(runtime, 0,
187d67c46b9STakashi Sakamoto 					 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
188d67c46b9STakashi Sakamoto end:
189d67c46b9STakashi Sakamoto 	return err;
190d67c46b9STakashi Sakamoto }
191d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
192d67c46b9STakashi Sakamoto 
193d67c46b9STakashi Sakamoto /**
194d67c46b9STakashi Sakamoto  * amdtp_stream_set_parameters - set stream parameters
195d67c46b9STakashi Sakamoto  * @s: the AMDTP stream to configure
196d67c46b9STakashi Sakamoto  * @rate: the sample rate
197df075feeSTakashi Sakamoto  * @data_block_quadlets: the size of a data block in quadlet unit
198d67c46b9STakashi Sakamoto  *
199d67c46b9STakashi Sakamoto  * The parameters must be set before the stream is started, and must not be
200d67c46b9STakashi Sakamoto  * changed while the stream is running.
201d67c46b9STakashi Sakamoto  */
202df075feeSTakashi Sakamoto int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
203df075feeSTakashi Sakamoto 				unsigned int data_block_quadlets)
204d67c46b9STakashi Sakamoto {
205df075feeSTakashi Sakamoto 	unsigned int sfc;
206d67c46b9STakashi Sakamoto 
207d67c46b9STakashi Sakamoto 	for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) {
208d67c46b9STakashi Sakamoto 		if (amdtp_rate_table[sfc] == rate)
209d67c46b9STakashi Sakamoto 			break;
210d67c46b9STakashi Sakamoto 	}
211d67c46b9STakashi Sakamoto 	if (sfc == ARRAY_SIZE(amdtp_rate_table))
212d67c46b9STakashi Sakamoto 		return -EINVAL;
213d67c46b9STakashi Sakamoto 
214d67c46b9STakashi Sakamoto 	s->sfc = sfc;
215df075feeSTakashi Sakamoto 	s->data_block_quadlets = data_block_quadlets;
216d67c46b9STakashi Sakamoto 	s->syt_interval = amdtp_syt_intervals[sfc];
217d67c46b9STakashi Sakamoto 
218d67c46b9STakashi Sakamoto 	/* default buffering in the device */
219d67c46b9STakashi Sakamoto 	s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
220d67c46b9STakashi Sakamoto 	if (s->flags & CIP_BLOCKING)
221d67c46b9STakashi Sakamoto 		/* additional buffering needed to adjust for no-data packets */
222d67c46b9STakashi Sakamoto 		s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
223d67c46b9STakashi Sakamoto 
224d67c46b9STakashi Sakamoto 	return 0;
225d67c46b9STakashi Sakamoto }
226d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_set_parameters);
227d67c46b9STakashi Sakamoto 
228d67c46b9STakashi Sakamoto /**
229d67c46b9STakashi Sakamoto  * amdtp_stream_get_max_payload - get the stream's packet size
230d67c46b9STakashi Sakamoto  * @s: the AMDTP stream
231d67c46b9STakashi Sakamoto  *
232d67c46b9STakashi Sakamoto  * This function must not be called before the stream has been configured
233d67c46b9STakashi Sakamoto  * with amdtp_stream_set_parameters().
234d67c46b9STakashi Sakamoto  */
235d67c46b9STakashi Sakamoto unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
236d67c46b9STakashi Sakamoto {
237d67c46b9STakashi Sakamoto 	unsigned int multiplier = 1;
2383b196c39STakashi Sakamoto 	unsigned int header_size = 0;
239d67c46b9STakashi Sakamoto 
240d67c46b9STakashi Sakamoto 	if (s->flags & CIP_JUMBO_PAYLOAD)
241d67c46b9STakashi Sakamoto 		multiplier = 5;
2423b196c39STakashi Sakamoto 	if (!(s->flags & CIP_NO_HEADER))
2433b196c39STakashi Sakamoto 		header_size = 8;
244d67c46b9STakashi Sakamoto 
2453b196c39STakashi Sakamoto 	return header_size +
2463b196c39STakashi Sakamoto 		s->syt_interval * s->data_block_quadlets * 4 * multiplier;
247d67c46b9STakashi Sakamoto }
248d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_get_max_payload);
249d67c46b9STakashi Sakamoto 
250d67c46b9STakashi Sakamoto /**
251d67c46b9STakashi Sakamoto  * amdtp_stream_pcm_prepare - prepare PCM device for running
252d67c46b9STakashi Sakamoto  * @s: the AMDTP stream
253d67c46b9STakashi Sakamoto  *
254d67c46b9STakashi Sakamoto  * This function should be called from the PCM device's .prepare callback.
255d67c46b9STakashi Sakamoto  */
256d67c46b9STakashi Sakamoto void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
257d67c46b9STakashi Sakamoto {
258d67c46b9STakashi Sakamoto 	tasklet_kill(&s->period_tasklet);
259d67c46b9STakashi Sakamoto 	s->pcm_buffer_pointer = 0;
260d67c46b9STakashi Sakamoto 	s->pcm_period_pointer = 0;
261d67c46b9STakashi Sakamoto }
262d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
263d67c46b9STakashi Sakamoto 
264d67c46b9STakashi Sakamoto static unsigned int calculate_data_blocks(struct amdtp_stream *s,
265d67c46b9STakashi Sakamoto 					  unsigned int syt)
266d67c46b9STakashi Sakamoto {
267d67c46b9STakashi Sakamoto 	unsigned int phase, data_blocks;
268d67c46b9STakashi Sakamoto 
269d67c46b9STakashi Sakamoto 	/* Blocking mode. */
270d67c46b9STakashi Sakamoto 	if (s->flags & CIP_BLOCKING) {
271d67c46b9STakashi Sakamoto 		/* This module generate empty packet for 'no data'. */
272d67c46b9STakashi Sakamoto 		if (syt == CIP_SYT_NO_INFO)
273d67c46b9STakashi Sakamoto 			data_blocks = 0;
274d67c46b9STakashi Sakamoto 		else
275d67c46b9STakashi Sakamoto 			data_blocks = s->syt_interval;
276d67c46b9STakashi Sakamoto 	/* Non-blocking mode. */
277d67c46b9STakashi Sakamoto 	} else {
278d67c46b9STakashi Sakamoto 		if (!cip_sfc_is_base_44100(s->sfc)) {
279d67c46b9STakashi Sakamoto 			/* Sample_rate / 8000 is an integer, and precomputed. */
280d67c46b9STakashi Sakamoto 			data_blocks = s->data_block_state;
281d67c46b9STakashi Sakamoto 		} else {
282d67c46b9STakashi Sakamoto 			phase = s->data_block_state;
283d67c46b9STakashi Sakamoto 
284d67c46b9STakashi Sakamoto 		/*
285d67c46b9STakashi Sakamoto 		 * This calculates the number of data blocks per packet so that
286d67c46b9STakashi Sakamoto 		 * 1) the overall rate is correct and exactly synchronized to
287d67c46b9STakashi Sakamoto 		 *    the bus clock, and
288d67c46b9STakashi Sakamoto 		 * 2) packets with a rounded-up number of blocks occur as early
289d67c46b9STakashi Sakamoto 		 *    as possible in the sequence (to prevent underruns of the
290d67c46b9STakashi Sakamoto 		 *    device's buffer).
291d67c46b9STakashi Sakamoto 		 */
292d67c46b9STakashi Sakamoto 			if (s->sfc == CIP_SFC_44100)
293d67c46b9STakashi Sakamoto 				/* 6 6 5 6 5 6 5 ... */
294d67c46b9STakashi Sakamoto 				data_blocks = 5 + ((phase & 1) ^
295d67c46b9STakashi Sakamoto 						   (phase == 0 || phase >= 40));
296d67c46b9STakashi Sakamoto 			else
297d67c46b9STakashi Sakamoto 				/* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
298d67c46b9STakashi Sakamoto 				data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
299d67c46b9STakashi Sakamoto 			if (++phase >= (80 >> (s->sfc >> 1)))
300d67c46b9STakashi Sakamoto 				phase = 0;
301d67c46b9STakashi Sakamoto 			s->data_block_state = phase;
302d67c46b9STakashi Sakamoto 		}
303d67c46b9STakashi Sakamoto 	}
304d67c46b9STakashi Sakamoto 
305d67c46b9STakashi Sakamoto 	return data_blocks;
306d67c46b9STakashi Sakamoto }
307d67c46b9STakashi Sakamoto 
308d67c46b9STakashi Sakamoto static unsigned int calculate_syt(struct amdtp_stream *s,
309d67c46b9STakashi Sakamoto 				  unsigned int cycle)
310d67c46b9STakashi Sakamoto {
311d67c46b9STakashi Sakamoto 	unsigned int syt_offset, phase, index, syt;
312d67c46b9STakashi Sakamoto 
313d67c46b9STakashi Sakamoto 	if (s->last_syt_offset < TICKS_PER_CYCLE) {
314d67c46b9STakashi Sakamoto 		if (!cip_sfc_is_base_44100(s->sfc))
315d67c46b9STakashi Sakamoto 			syt_offset = s->last_syt_offset + s->syt_offset_state;
316d67c46b9STakashi Sakamoto 		else {
317d67c46b9STakashi Sakamoto 		/*
318d67c46b9STakashi Sakamoto 		 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
319d67c46b9STakashi Sakamoto 		 *   n * SYT_INTERVAL * 24576000 / sample_rate
320d67c46b9STakashi Sakamoto 		 * Modulo TICKS_PER_CYCLE, the difference between successive
321d67c46b9STakashi Sakamoto 		 * elements is about 1386.23.  Rounding the results of this
322d67c46b9STakashi Sakamoto 		 * formula to the SYT precision results in a sequence of
323d67c46b9STakashi Sakamoto 		 * differences that begins with:
324d67c46b9STakashi Sakamoto 		 *   1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
325d67c46b9STakashi Sakamoto 		 * This code generates _exactly_ the same sequence.
326d67c46b9STakashi Sakamoto 		 */
327d67c46b9STakashi Sakamoto 			phase = s->syt_offset_state;
328d67c46b9STakashi Sakamoto 			index = phase % 13;
329d67c46b9STakashi Sakamoto 			syt_offset = s->last_syt_offset;
330d67c46b9STakashi Sakamoto 			syt_offset += 1386 + ((index && !(index & 3)) ||
331d67c46b9STakashi Sakamoto 					      phase == 146);
332d67c46b9STakashi Sakamoto 			if (++phase >= 147)
333d67c46b9STakashi Sakamoto 				phase = 0;
334d67c46b9STakashi Sakamoto 			s->syt_offset_state = phase;
335d67c46b9STakashi Sakamoto 		}
336d67c46b9STakashi Sakamoto 	} else
337d67c46b9STakashi Sakamoto 		syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
338d67c46b9STakashi Sakamoto 	s->last_syt_offset = syt_offset;
339d67c46b9STakashi Sakamoto 
340d67c46b9STakashi Sakamoto 	if (syt_offset < TICKS_PER_CYCLE) {
341d67c46b9STakashi Sakamoto 		syt_offset += s->transfer_delay;
342d67c46b9STakashi Sakamoto 		syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
343d67c46b9STakashi Sakamoto 		syt += syt_offset % TICKS_PER_CYCLE;
344d67c46b9STakashi Sakamoto 
345d67c46b9STakashi Sakamoto 		return syt & CIP_SYT_MASK;
346d67c46b9STakashi Sakamoto 	} else {
347d67c46b9STakashi Sakamoto 		return CIP_SYT_NO_INFO;
348d67c46b9STakashi Sakamoto 	}
349d67c46b9STakashi Sakamoto }
350d67c46b9STakashi Sakamoto 
351d67c46b9STakashi Sakamoto static void update_pcm_pointers(struct amdtp_stream *s,
352d67c46b9STakashi Sakamoto 				struct snd_pcm_substream *pcm,
353d67c46b9STakashi Sakamoto 				unsigned int frames)
354d67c46b9STakashi Sakamoto {
355d67c46b9STakashi Sakamoto 	unsigned int ptr;
356d67c46b9STakashi Sakamoto 
357d67c46b9STakashi Sakamoto 	ptr = s->pcm_buffer_pointer + frames;
358d67c46b9STakashi Sakamoto 	if (ptr >= pcm->runtime->buffer_size)
359d67c46b9STakashi Sakamoto 		ptr -= pcm->runtime->buffer_size;
360d67c46b9STakashi Sakamoto 	ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
361d67c46b9STakashi Sakamoto 
362d67c46b9STakashi Sakamoto 	s->pcm_period_pointer += frames;
363d67c46b9STakashi Sakamoto 	if (s->pcm_period_pointer >= pcm->runtime->period_size) {
364d67c46b9STakashi Sakamoto 		s->pcm_period_pointer -= pcm->runtime->period_size;
365d67c46b9STakashi Sakamoto 		tasklet_hi_schedule(&s->period_tasklet);
366d67c46b9STakashi Sakamoto 	}
367d67c46b9STakashi Sakamoto }
368d67c46b9STakashi Sakamoto 
369d67c46b9STakashi Sakamoto static void pcm_period_tasklet(unsigned long data)
370d67c46b9STakashi Sakamoto {
371d67c46b9STakashi Sakamoto 	struct amdtp_stream *s = (void *)data;
372d67c46b9STakashi Sakamoto 	struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
373d67c46b9STakashi Sakamoto 
374d67c46b9STakashi Sakamoto 	if (pcm)
375d67c46b9STakashi Sakamoto 		snd_pcm_period_elapsed(pcm);
376d67c46b9STakashi Sakamoto }
377d67c46b9STakashi Sakamoto 
378ff38e0c7STakashi Sakamoto static int queue_packet(struct amdtp_stream *s, unsigned int header_length,
379ff38e0c7STakashi Sakamoto 			unsigned int payload_length)
380d67c46b9STakashi Sakamoto {
381d67c46b9STakashi Sakamoto 	struct fw_iso_packet p = {0};
382d67c46b9STakashi Sakamoto 	int err = 0;
383d67c46b9STakashi Sakamoto 
384d67c46b9STakashi Sakamoto 	if (IS_ERR(s->context))
385d67c46b9STakashi Sakamoto 		goto end;
386d67c46b9STakashi Sakamoto 
387d67c46b9STakashi Sakamoto 	p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
3883b196c39STakashi Sakamoto 	p.tag = s->tag;
389d67c46b9STakashi Sakamoto 	p.header_length = header_length;
390ff38e0c7STakashi Sakamoto 	if (payload_length > 0)
391ff38e0c7STakashi Sakamoto 		p.payload_length = payload_length;
392ff38e0c7STakashi Sakamoto 	else
393ff38e0c7STakashi Sakamoto 		p.skip = true;
394d67c46b9STakashi Sakamoto 	err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
395d67c46b9STakashi Sakamoto 				   s->buffer.packets[s->packet_index].offset);
396d67c46b9STakashi Sakamoto 	if (err < 0) {
397d67c46b9STakashi Sakamoto 		dev_err(&s->unit->device, "queueing error: %d\n", err);
398d67c46b9STakashi Sakamoto 		goto end;
399d67c46b9STakashi Sakamoto 	}
400d67c46b9STakashi Sakamoto 
401d67c46b9STakashi Sakamoto 	if (++s->packet_index >= QUEUE_LENGTH)
402d67c46b9STakashi Sakamoto 		s->packet_index = 0;
403d67c46b9STakashi Sakamoto end:
404d67c46b9STakashi Sakamoto 	return err;
405d67c46b9STakashi Sakamoto }
406d67c46b9STakashi Sakamoto 
407d67c46b9STakashi Sakamoto static inline int queue_out_packet(struct amdtp_stream *s,
408ff38e0c7STakashi Sakamoto 				   unsigned int payload_length)
409d67c46b9STakashi Sakamoto {
410ff38e0c7STakashi Sakamoto 	return queue_packet(s, OUT_PACKET_HEADER_SIZE, payload_length);
411d67c46b9STakashi Sakamoto }
412d67c46b9STakashi Sakamoto 
413d67c46b9STakashi Sakamoto static inline int queue_in_packet(struct amdtp_stream *s)
414d67c46b9STakashi Sakamoto {
415d67c46b9STakashi Sakamoto 	return queue_packet(s, IN_PACKET_HEADER_SIZE,
416ff38e0c7STakashi Sakamoto 			    amdtp_stream_get_max_payload(s));
417d67c46b9STakashi Sakamoto }
418d67c46b9STakashi Sakamoto 
419ff0fb5aaSTakashi Sakamoto static int handle_out_packet(struct amdtp_stream *s,
420ff0fb5aaSTakashi Sakamoto 			     unsigned int payload_length, unsigned int cycle,
421a9c4284bSTakashi Sakamoto 			     unsigned int index)
422d67c46b9STakashi Sakamoto {
423d67c46b9STakashi Sakamoto 	__be32 *buffer;
424390a1512STakashi Sakamoto 	unsigned int syt;
425390a1512STakashi Sakamoto 	unsigned int data_blocks;
426d67c46b9STakashi Sakamoto 	unsigned int pcm_frames;
427d67c46b9STakashi Sakamoto 	struct snd_pcm_substream *pcm;
428d67c46b9STakashi Sakamoto 
429d67c46b9STakashi Sakamoto 	buffer = s->buffer.packets[s->packet_index].buffer;
430390a1512STakashi Sakamoto 	syt = calculate_syt(s, cycle);
431390a1512STakashi Sakamoto 	data_blocks = calculate_data_blocks(s, syt);
432df075feeSTakashi Sakamoto 	pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt);
433d67c46b9STakashi Sakamoto 
4349dae017bSTakashi Sakamoto 	if (s->flags & CIP_DBC_IS_END_EVENT)
4359dae017bSTakashi Sakamoto 		s->data_block_counter =
4369dae017bSTakashi Sakamoto 				(s->data_block_counter + data_blocks) & 0xff;
4379dae017bSTakashi Sakamoto 
438d67c46b9STakashi Sakamoto 	buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
439d67c46b9STakashi Sakamoto 				(s->data_block_quadlets << CIP_DBS_SHIFT) |
4409863874fSTakashi Sakamoto 				((s->sph << CIP_SPH_SHIFT) & CIP_SPH_MASK) |
441d67c46b9STakashi Sakamoto 				s->data_block_counter);
442d67c46b9STakashi Sakamoto 	buffer[1] = cpu_to_be32(CIP_EOH |
443d67c46b9STakashi Sakamoto 				((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) |
444d67c46b9STakashi Sakamoto 				((s->fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) |
445d67c46b9STakashi Sakamoto 				(syt & CIP_SYT_MASK));
446d67c46b9STakashi Sakamoto 
4479dae017bSTakashi Sakamoto 	if (!(s->flags & CIP_DBC_IS_END_EVENT))
4489dae017bSTakashi Sakamoto 		s->data_block_counter =
4499dae017bSTakashi Sakamoto 				(s->data_block_counter + data_blocks) & 0xff;
450d67c46b9STakashi Sakamoto 	payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
4510c95c1d6STakashi Sakamoto 
452a9c4284bSTakashi Sakamoto 	trace_out_packet(s, cycle, buffer, payload_length, index);
4530c95c1d6STakashi Sakamoto 
454ff38e0c7STakashi Sakamoto 	if (queue_out_packet(s, payload_length) < 0)
455d67c46b9STakashi Sakamoto 		return -EIO;
456d67c46b9STakashi Sakamoto 
457d67c46b9STakashi Sakamoto 	pcm = ACCESS_ONCE(s->pcm);
458d67c46b9STakashi Sakamoto 	if (pcm && pcm_frames > 0)
459d67c46b9STakashi Sakamoto 		update_pcm_pointers(s, pcm, pcm_frames);
460d67c46b9STakashi Sakamoto 
461d67c46b9STakashi Sakamoto 	/* No need to return the number of handled data blocks. */
462d67c46b9STakashi Sakamoto 	return 0;
463d67c46b9STakashi Sakamoto }
464d67c46b9STakashi Sakamoto 
4653b196c39STakashi Sakamoto static int handle_out_packet_without_header(struct amdtp_stream *s,
4663b196c39STakashi Sakamoto 			unsigned int payload_length, unsigned int cycle,
4673b196c39STakashi Sakamoto 			unsigned int index)
4683b196c39STakashi Sakamoto {
4693b196c39STakashi Sakamoto 	__be32 *buffer;
4703b196c39STakashi Sakamoto 	unsigned int syt;
4713b196c39STakashi Sakamoto 	unsigned int data_blocks;
4723b196c39STakashi Sakamoto 	unsigned int pcm_frames;
4733b196c39STakashi Sakamoto 	struct snd_pcm_substream *pcm;
4743b196c39STakashi Sakamoto 
4753b196c39STakashi Sakamoto 	buffer = s->buffer.packets[s->packet_index].buffer;
4763b196c39STakashi Sakamoto 	syt = calculate_syt(s, cycle);
4773b196c39STakashi Sakamoto 	data_blocks = calculate_data_blocks(s, syt);
4783b196c39STakashi Sakamoto 	pcm_frames = s->process_data_blocks(s, buffer, data_blocks, &syt);
4793b196c39STakashi Sakamoto 	s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
4803b196c39STakashi Sakamoto 
4813b196c39STakashi Sakamoto 	payload_length = data_blocks * 4 * s->data_block_quadlets;
482*b164d2fdSTakashi Sakamoto 
483*b164d2fdSTakashi Sakamoto 	trace_out_packet_without_header(s, cycle, payload_length, data_blocks,
484*b164d2fdSTakashi Sakamoto 					index);
485*b164d2fdSTakashi Sakamoto 
4863b196c39STakashi Sakamoto 	if (queue_out_packet(s, payload_length) < 0)
4873b196c39STakashi Sakamoto 		return -EIO;
4883b196c39STakashi Sakamoto 
4893b196c39STakashi Sakamoto 	pcm = ACCESS_ONCE(s->pcm);
4903b196c39STakashi Sakamoto 	if (pcm && pcm_frames > 0)
4913b196c39STakashi Sakamoto 		update_pcm_pointers(s, pcm, pcm_frames);
4923b196c39STakashi Sakamoto 
4933b196c39STakashi Sakamoto 	/* No need to return the number of handled data blocks. */
4943b196c39STakashi Sakamoto 	return 0;
4953b196c39STakashi Sakamoto }
4963b196c39STakashi Sakamoto 
497d67c46b9STakashi Sakamoto static int handle_in_packet(struct amdtp_stream *s,
498ff0fb5aaSTakashi Sakamoto 			    unsigned int payload_length, unsigned int cycle,
499a9c4284bSTakashi Sakamoto 			    unsigned int index)
500d67c46b9STakashi Sakamoto {
501d9a16fc9STakashi Sakamoto 	__be32 *buffer;
502d67c46b9STakashi Sakamoto 	u32 cip_header[2];
5039863874fSTakashi Sakamoto 	unsigned int sph, fmt, fdf, syt;
504d67c46b9STakashi Sakamoto 	unsigned int data_block_quadlets, data_block_counter, dbc_interval;
505d9a16fc9STakashi Sakamoto 	unsigned int data_blocks;
506d67c46b9STakashi Sakamoto 	struct snd_pcm_substream *pcm;
507d67c46b9STakashi Sakamoto 	unsigned int pcm_frames;
508d67c46b9STakashi Sakamoto 	bool lost;
509d67c46b9STakashi Sakamoto 
510d9a16fc9STakashi Sakamoto 	buffer = s->buffer.packets[s->packet_index].buffer;
511d67c46b9STakashi Sakamoto 	cip_header[0] = be32_to_cpu(buffer[0]);
512d67c46b9STakashi Sakamoto 	cip_header[1] = be32_to_cpu(buffer[1]);
513d67c46b9STakashi Sakamoto 
514ff0fb5aaSTakashi Sakamoto 	trace_in_packet(s, cycle, cip_header, payload_length, index);
5150c95c1d6STakashi Sakamoto 
516d67c46b9STakashi Sakamoto 	/*
517d67c46b9STakashi Sakamoto 	 * This module supports 'Two-quadlet CIP header with SYT field'.
518d67c46b9STakashi Sakamoto 	 * For convenience, also check FMT field is AM824 or not.
519d67c46b9STakashi Sakamoto 	 */
5202128f78fSTakashi Sakamoto 	if ((((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
5212128f78fSTakashi Sakamoto 	     ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) &&
5222128f78fSTakashi Sakamoto 	    (!(s->flags & CIP_HEADER_WITHOUT_EOH))) {
523d67c46b9STakashi Sakamoto 		dev_info_ratelimited(&s->unit->device,
524d67c46b9STakashi Sakamoto 				"Invalid CIP header for AMDTP: %08X:%08X\n",
525d67c46b9STakashi Sakamoto 				cip_header[0], cip_header[1]);
526d9a16fc9STakashi Sakamoto 		data_blocks = 0;
527d67c46b9STakashi Sakamoto 		pcm_frames = 0;
528d67c46b9STakashi Sakamoto 		goto end;
529d67c46b9STakashi Sakamoto 	}
530d67c46b9STakashi Sakamoto 
531d67c46b9STakashi Sakamoto 	/* Check valid protocol or not. */
5329863874fSTakashi Sakamoto 	sph = (cip_header[0] & CIP_SPH_MASK) >> CIP_SPH_SHIFT;
533d67c46b9STakashi Sakamoto 	fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT;
5349863874fSTakashi Sakamoto 	if (sph != s->sph || fmt != s->fmt) {
5352a7e1713STakashi Sakamoto 		dev_info_ratelimited(&s->unit->device,
536d67c46b9STakashi Sakamoto 				     "Detect unexpected protocol: %08x %08x\n",
537d67c46b9STakashi Sakamoto 				     cip_header[0], cip_header[1]);
538d9a16fc9STakashi Sakamoto 		data_blocks = 0;
5392a7e1713STakashi Sakamoto 		pcm_frames = 0;
5402a7e1713STakashi Sakamoto 		goto end;
541d67c46b9STakashi Sakamoto 	}
542d67c46b9STakashi Sakamoto 
543d67c46b9STakashi Sakamoto 	/* Calculate data blocks */
544d67c46b9STakashi Sakamoto 	fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT;
545ff0fb5aaSTakashi Sakamoto 	if (payload_length < 12 ||
546d67c46b9STakashi Sakamoto 	    (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) {
547d9a16fc9STakashi Sakamoto 		data_blocks = 0;
548d67c46b9STakashi Sakamoto 	} else {
549d67c46b9STakashi Sakamoto 		data_block_quadlets =
550d67c46b9STakashi Sakamoto 			(cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT;
551d67c46b9STakashi Sakamoto 		/* avoid division by zero */
552d67c46b9STakashi Sakamoto 		if (data_block_quadlets == 0) {
553d67c46b9STakashi Sakamoto 			dev_err(&s->unit->device,
554d67c46b9STakashi Sakamoto 				"Detect invalid value in dbs field: %08X\n",
555d67c46b9STakashi Sakamoto 				cip_header[0]);
556d67c46b9STakashi Sakamoto 			return -EPROTO;
557d67c46b9STakashi Sakamoto 		}
558d67c46b9STakashi Sakamoto 		if (s->flags & CIP_WRONG_DBS)
559d67c46b9STakashi Sakamoto 			data_block_quadlets = s->data_block_quadlets;
560d67c46b9STakashi Sakamoto 
561ff0fb5aaSTakashi Sakamoto 		data_blocks = (payload_length / 4 - 2) /
562ff0fb5aaSTakashi Sakamoto 							data_block_quadlets;
563d67c46b9STakashi Sakamoto 	}
564d67c46b9STakashi Sakamoto 
565d67c46b9STakashi Sakamoto 	/* Check data block counter continuity */
566d67c46b9STakashi Sakamoto 	data_block_counter = cip_header[0] & CIP_DBC_MASK;
567d9a16fc9STakashi Sakamoto 	if (data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
568d67c46b9STakashi Sakamoto 	    s->data_block_counter != UINT_MAX)
569d67c46b9STakashi Sakamoto 		data_block_counter = s->data_block_counter;
570d67c46b9STakashi Sakamoto 
571d67c46b9STakashi Sakamoto 	if (((s->flags & CIP_SKIP_DBC_ZERO_CHECK) &&
572d67c46b9STakashi Sakamoto 	     data_block_counter == s->tx_first_dbc) ||
573d67c46b9STakashi Sakamoto 	    s->data_block_counter == UINT_MAX) {
574d67c46b9STakashi Sakamoto 		lost = false;
575d67c46b9STakashi Sakamoto 	} else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
576d67c46b9STakashi Sakamoto 		lost = data_block_counter != s->data_block_counter;
577d67c46b9STakashi Sakamoto 	} else {
578d9a16fc9STakashi Sakamoto 		if (data_blocks > 0 && s->tx_dbc_interval > 0)
579d67c46b9STakashi Sakamoto 			dbc_interval = s->tx_dbc_interval;
580d67c46b9STakashi Sakamoto 		else
581d9a16fc9STakashi Sakamoto 			dbc_interval = data_blocks;
582d67c46b9STakashi Sakamoto 
583d67c46b9STakashi Sakamoto 		lost = data_block_counter !=
584d67c46b9STakashi Sakamoto 		       ((s->data_block_counter + dbc_interval) & 0xff);
585d67c46b9STakashi Sakamoto 	}
586d67c46b9STakashi Sakamoto 
587d67c46b9STakashi Sakamoto 	if (lost) {
588d67c46b9STakashi Sakamoto 		dev_err(&s->unit->device,
589d67c46b9STakashi Sakamoto 			"Detect discontinuity of CIP: %02X %02X\n",
590d67c46b9STakashi Sakamoto 			s->data_block_counter, data_block_counter);
591d67c46b9STakashi Sakamoto 		return -EIO;
592d67c46b9STakashi Sakamoto 	}
593d67c46b9STakashi Sakamoto 
594d9a16fc9STakashi Sakamoto 	syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK;
595d9a16fc9STakashi Sakamoto 	pcm_frames = s->process_data_blocks(s, buffer + 2, data_blocks, &syt);
596d67c46b9STakashi Sakamoto 
597d67c46b9STakashi Sakamoto 	if (s->flags & CIP_DBC_IS_END_EVENT)
598d67c46b9STakashi Sakamoto 		s->data_block_counter = data_block_counter;
599d67c46b9STakashi Sakamoto 	else
600d67c46b9STakashi Sakamoto 		s->data_block_counter =
601d9a16fc9STakashi Sakamoto 				(data_block_counter + data_blocks) & 0xff;
602d67c46b9STakashi Sakamoto end:
603d67c46b9STakashi Sakamoto 	if (queue_in_packet(s) < 0)
604d67c46b9STakashi Sakamoto 		return -EIO;
605d67c46b9STakashi Sakamoto 
606d67c46b9STakashi Sakamoto 	pcm = ACCESS_ONCE(s->pcm);
607d67c46b9STakashi Sakamoto 	if (pcm && pcm_frames > 0)
608d67c46b9STakashi Sakamoto 		update_pcm_pointers(s, pcm, pcm_frames);
609d67c46b9STakashi Sakamoto 
610d67c46b9STakashi Sakamoto 	return 0;
611d67c46b9STakashi Sakamoto }
612d67c46b9STakashi Sakamoto 
6133b196c39STakashi Sakamoto static int handle_in_packet_without_header(struct amdtp_stream *s,
6143b196c39STakashi Sakamoto 			unsigned int payload_quadlets, unsigned int cycle,
6153b196c39STakashi Sakamoto 			unsigned int index)
6163b196c39STakashi Sakamoto {
6173b196c39STakashi Sakamoto 	__be32 *buffer;
6183b196c39STakashi Sakamoto 	unsigned int data_blocks;
6193b196c39STakashi Sakamoto 	struct snd_pcm_substream *pcm;
6203b196c39STakashi Sakamoto 	unsigned int pcm_frames;
6213b196c39STakashi Sakamoto 
6223b196c39STakashi Sakamoto 	buffer = s->buffer.packets[s->packet_index].buffer;
6233b196c39STakashi Sakamoto 	data_blocks = payload_quadlets / s->data_block_quadlets;
624*b164d2fdSTakashi Sakamoto 
625*b164d2fdSTakashi Sakamoto 	trace_in_packet_without_header(s, cycle, payload_quadlets, data_blocks,
626*b164d2fdSTakashi Sakamoto 				       index);
627*b164d2fdSTakashi Sakamoto 
6283b196c39STakashi Sakamoto 	pcm_frames = s->process_data_blocks(s, buffer, data_blocks, NULL);
6293b196c39STakashi Sakamoto 	s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
6303b196c39STakashi Sakamoto 
6313b196c39STakashi Sakamoto 	if (queue_in_packet(s) < 0)
6323b196c39STakashi Sakamoto 		return -EIO;
6333b196c39STakashi Sakamoto 
6343b196c39STakashi Sakamoto 	pcm = ACCESS_ONCE(s->pcm);
6353b196c39STakashi Sakamoto 	if (pcm && pcm_frames > 0)
6363b196c39STakashi Sakamoto 		update_pcm_pointers(s, pcm, pcm_frames);
6373b196c39STakashi Sakamoto 
6383b196c39STakashi Sakamoto 	return 0;
6393b196c39STakashi Sakamoto }
6403b196c39STakashi Sakamoto 
64173fc7f08STakashi Sakamoto /*
64273fc7f08STakashi Sakamoto  * In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On
64373fc7f08STakashi Sakamoto  * the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent
64473fc7f08STakashi Sakamoto  * it. Thus, via Linux firewire subsystem, we can get the 3 bits for second.
64573fc7f08STakashi Sakamoto  */
64673fc7f08STakashi Sakamoto static inline u32 compute_cycle_count(u32 tstamp)
64773fc7f08STakashi Sakamoto {
64873fc7f08STakashi Sakamoto 	return (((tstamp >> 13) & 0x07) * 8000) + (tstamp & 0x1fff);
64973fc7f08STakashi Sakamoto }
65073fc7f08STakashi Sakamoto 
65173fc7f08STakashi Sakamoto static inline u32 increment_cycle_count(u32 cycle, unsigned int addend)
65273fc7f08STakashi Sakamoto {
65373fc7f08STakashi Sakamoto 	cycle += addend;
65473fc7f08STakashi Sakamoto 	if (cycle >= 8 * CYCLES_PER_SECOND)
65573fc7f08STakashi Sakamoto 		cycle -= 8 * CYCLES_PER_SECOND;
65673fc7f08STakashi Sakamoto 	return cycle;
65773fc7f08STakashi Sakamoto }
65873fc7f08STakashi Sakamoto 
659f90e2dedSTakashi Sakamoto static inline u32 decrement_cycle_count(u32 cycle, unsigned int subtrahend)
660f90e2dedSTakashi Sakamoto {
661f90e2dedSTakashi Sakamoto 	if (cycle < subtrahend)
662f90e2dedSTakashi Sakamoto 		cycle += 8 * CYCLES_PER_SECOND;
663f90e2dedSTakashi Sakamoto 	return cycle - subtrahend;
664f90e2dedSTakashi Sakamoto }
665f90e2dedSTakashi Sakamoto 
66673fc7f08STakashi Sakamoto static void out_stream_callback(struct fw_iso_context *context, u32 tstamp,
667d67c46b9STakashi Sakamoto 				size_t header_length, void *header,
668d67c46b9STakashi Sakamoto 				void *private_data)
669d67c46b9STakashi Sakamoto {
670d67c46b9STakashi Sakamoto 	struct amdtp_stream *s = private_data;
671390a1512STakashi Sakamoto 	unsigned int i, packets = header_length / 4;
67273fc7f08STakashi Sakamoto 	u32 cycle;
673d67c46b9STakashi Sakamoto 
674d67c46b9STakashi Sakamoto 	if (s->packet_index < 0)
675d67c46b9STakashi Sakamoto 		return;
676d67c46b9STakashi Sakamoto 
67773fc7f08STakashi Sakamoto 	cycle = compute_cycle_count(tstamp);
67873fc7f08STakashi Sakamoto 
67973fc7f08STakashi Sakamoto 	/* Align to actual cycle count for the last packet. */
68073fc7f08STakashi Sakamoto 	cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets);
681d67c46b9STakashi Sakamoto 
682d67c46b9STakashi Sakamoto 	for (i = 0; i < packets; ++i) {
68373fc7f08STakashi Sakamoto 		cycle = increment_cycle_count(cycle, 1);
6843b196c39STakashi Sakamoto 		if (s->handle_packet(s, 0, cycle, i) < 0) {
685d67c46b9STakashi Sakamoto 			s->packet_index = -1;
686d67c46b9STakashi Sakamoto 			amdtp_stream_pcm_abort(s);
687d67c46b9STakashi Sakamoto 			return;
688d67c46b9STakashi Sakamoto 		}
689d67c46b9STakashi Sakamoto 	}
690d67c46b9STakashi Sakamoto 
691d67c46b9STakashi Sakamoto 	fw_iso_context_queue_flush(s->context);
692d67c46b9STakashi Sakamoto }
693d67c46b9STakashi Sakamoto 
69473fc7f08STakashi Sakamoto static void in_stream_callback(struct fw_iso_context *context, u32 tstamp,
695d67c46b9STakashi Sakamoto 			       size_t header_length, void *header,
696d67c46b9STakashi Sakamoto 			       void *private_data)
697d67c46b9STakashi Sakamoto {
698d67c46b9STakashi Sakamoto 	struct amdtp_stream *s = private_data;
699d9a16fc9STakashi Sakamoto 	unsigned int i, packets;
700ff0fb5aaSTakashi Sakamoto 	unsigned int payload_length, max_payload_length;
701d9a16fc9STakashi Sakamoto 	__be32 *headers = header;
702f90e2dedSTakashi Sakamoto 	u32 cycle;
703d67c46b9STakashi Sakamoto 
704d67c46b9STakashi Sakamoto 	if (s->packet_index < 0)
705d67c46b9STakashi Sakamoto 		return;
706d67c46b9STakashi Sakamoto 
707d67c46b9STakashi Sakamoto 	/* The number of packets in buffer */
708d67c46b9STakashi Sakamoto 	packets = header_length / IN_PACKET_HEADER_SIZE;
709d67c46b9STakashi Sakamoto 
710f90e2dedSTakashi Sakamoto 	cycle = compute_cycle_count(tstamp);
711f90e2dedSTakashi Sakamoto 
712f90e2dedSTakashi Sakamoto 	/* Align to actual cycle count for the last packet. */
713f90e2dedSTakashi Sakamoto 	cycle = decrement_cycle_count(cycle, packets);
714f90e2dedSTakashi Sakamoto 
715d67c46b9STakashi Sakamoto 	/* For buffer-over-run prevention. */
716ff0fb5aaSTakashi Sakamoto 	max_payload_length = amdtp_stream_get_max_payload(s);
717d67c46b9STakashi Sakamoto 
718d9a16fc9STakashi Sakamoto 	for (i = 0; i < packets; i++) {
719f90e2dedSTakashi Sakamoto 		cycle = increment_cycle_count(cycle, 1);
720d67c46b9STakashi Sakamoto 
721d67c46b9STakashi Sakamoto 		/* The number of quadlets in this packet */
722ff0fb5aaSTakashi Sakamoto 		payload_length =
723ff0fb5aaSTakashi Sakamoto 			(be32_to_cpu(headers[i]) >> ISO_DATA_LENGTH_SHIFT);
724ff0fb5aaSTakashi Sakamoto 		if (payload_length > max_payload_length) {
725d67c46b9STakashi Sakamoto 			dev_err(&s->unit->device,
726ff0fb5aaSTakashi Sakamoto 				"Detect jumbo payload: %04x %04x\n",
727ff0fb5aaSTakashi Sakamoto 				payload_length, max_payload_length);
728d67c46b9STakashi Sakamoto 			break;
729d67c46b9STakashi Sakamoto 		}
730d67c46b9STakashi Sakamoto 
7313b196c39STakashi Sakamoto 		if (s->handle_packet(s, payload_length, cycle, i) < 0)
732d67c46b9STakashi Sakamoto 			break;
733d67c46b9STakashi Sakamoto 	}
734d67c46b9STakashi Sakamoto 
735dec63cc8STakashi Sakamoto 	/* Queueing error or detecting invalid payload. */
736d9a16fc9STakashi Sakamoto 	if (i < packets) {
737d67c46b9STakashi Sakamoto 		s->packet_index = -1;
738d67c46b9STakashi Sakamoto 		amdtp_stream_pcm_abort(s);
739d67c46b9STakashi Sakamoto 		return;
740d67c46b9STakashi Sakamoto 	}
741d67c46b9STakashi Sakamoto 
742d67c46b9STakashi Sakamoto 	fw_iso_context_queue_flush(s->context);
743d67c46b9STakashi Sakamoto }
744d67c46b9STakashi Sakamoto 
745d67c46b9STakashi Sakamoto /* this is executed one time */
746d67c46b9STakashi Sakamoto static void amdtp_stream_first_callback(struct fw_iso_context *context,
74773fc7f08STakashi Sakamoto 					u32 tstamp, size_t header_length,
748d67c46b9STakashi Sakamoto 					void *header, void *private_data)
749d67c46b9STakashi Sakamoto {
750d67c46b9STakashi Sakamoto 	struct amdtp_stream *s = private_data;
751a04513f8STakashi Sakamoto 	u32 cycle;
752a04513f8STakashi Sakamoto 	unsigned int packets;
753d67c46b9STakashi Sakamoto 
754d67c46b9STakashi Sakamoto 	/*
755d67c46b9STakashi Sakamoto 	 * For in-stream, first packet has come.
756d67c46b9STakashi Sakamoto 	 * For out-stream, prepared to transmit first packet
757d67c46b9STakashi Sakamoto 	 */
758d67c46b9STakashi Sakamoto 	s->callbacked = true;
759d67c46b9STakashi Sakamoto 	wake_up(&s->callback_wait);
760d67c46b9STakashi Sakamoto 
761a04513f8STakashi Sakamoto 	cycle = compute_cycle_count(tstamp);
762a04513f8STakashi Sakamoto 
763a04513f8STakashi Sakamoto 	if (s->direction == AMDTP_IN_STREAM) {
764a04513f8STakashi Sakamoto 		packets = header_length / IN_PACKET_HEADER_SIZE;
765a04513f8STakashi Sakamoto 		cycle = decrement_cycle_count(cycle, packets);
766d67c46b9STakashi Sakamoto 		context->callback.sc = in_stream_callback;
7673b196c39STakashi Sakamoto 		if (s->flags & CIP_NO_HEADER)
7683b196c39STakashi Sakamoto 			s->handle_packet = handle_in_packet_without_header;
7693b196c39STakashi Sakamoto 		else
7703b196c39STakashi Sakamoto 			s->handle_packet = handle_in_packet;
771a04513f8STakashi Sakamoto 	} else {
772a04513f8STakashi Sakamoto 		packets = header_length / 4;
773a04513f8STakashi Sakamoto 		cycle = increment_cycle_count(cycle, QUEUE_LENGTH - packets);
774d67c46b9STakashi Sakamoto 		context->callback.sc = out_stream_callback;
7753b196c39STakashi Sakamoto 		if (s->flags & CIP_NO_HEADER)
7763b196c39STakashi Sakamoto 			s->handle_packet = handle_out_packet_without_header;
7773b196c39STakashi Sakamoto 		else
7783b196c39STakashi Sakamoto 			s->handle_packet = handle_out_packet;
779a04513f8STakashi Sakamoto 	}
780a04513f8STakashi Sakamoto 
781a04513f8STakashi Sakamoto 	s->start_cycle = cycle;
782d67c46b9STakashi Sakamoto 
78373fc7f08STakashi Sakamoto 	context->callback.sc(context, tstamp, header_length, header, s);
784d67c46b9STakashi Sakamoto }
785d67c46b9STakashi Sakamoto 
786d67c46b9STakashi Sakamoto /**
787d67c46b9STakashi Sakamoto  * amdtp_stream_start - start transferring packets
788d67c46b9STakashi Sakamoto  * @s: the AMDTP stream to start
789d67c46b9STakashi Sakamoto  * @channel: the isochronous channel on the bus
790d67c46b9STakashi Sakamoto  * @speed: firewire speed code
791d67c46b9STakashi Sakamoto  *
792d67c46b9STakashi Sakamoto  * The stream cannot be started until it has been configured with
793d67c46b9STakashi Sakamoto  * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
794d67c46b9STakashi Sakamoto  * device can be started.
795d67c46b9STakashi Sakamoto  */
796d67c46b9STakashi Sakamoto int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
797d67c46b9STakashi Sakamoto {
798d67c46b9STakashi Sakamoto 	static const struct {
799d67c46b9STakashi Sakamoto 		unsigned int data_block;
800d67c46b9STakashi Sakamoto 		unsigned int syt_offset;
801d67c46b9STakashi Sakamoto 	} initial_state[] = {
802d67c46b9STakashi Sakamoto 		[CIP_SFC_32000]  = {  4, 3072 },
803d67c46b9STakashi Sakamoto 		[CIP_SFC_48000]  = {  6, 1024 },
804d67c46b9STakashi Sakamoto 		[CIP_SFC_96000]  = { 12, 1024 },
805d67c46b9STakashi Sakamoto 		[CIP_SFC_192000] = { 24, 1024 },
806d67c46b9STakashi Sakamoto 		[CIP_SFC_44100]  = {  0,   67 },
807d67c46b9STakashi Sakamoto 		[CIP_SFC_88200]  = {  0,   67 },
808d67c46b9STakashi Sakamoto 		[CIP_SFC_176400] = {  0,   67 },
809d67c46b9STakashi Sakamoto 	};
810d67c46b9STakashi Sakamoto 	unsigned int header_size;
811d67c46b9STakashi Sakamoto 	enum dma_data_direction dir;
812d67c46b9STakashi Sakamoto 	int type, tag, err;
813d67c46b9STakashi Sakamoto 
814d67c46b9STakashi Sakamoto 	mutex_lock(&s->mutex);
815d67c46b9STakashi Sakamoto 
816d67c46b9STakashi Sakamoto 	if (WARN_ON(amdtp_stream_running(s) ||
817d67c46b9STakashi Sakamoto 		    (s->data_block_quadlets < 1))) {
818d67c46b9STakashi Sakamoto 		err = -EBADFD;
819d67c46b9STakashi Sakamoto 		goto err_unlock;
820d67c46b9STakashi Sakamoto 	}
821d67c46b9STakashi Sakamoto 
82262f00e40STakashi Sakamoto 	if (s->direction == AMDTP_IN_STREAM)
823d67c46b9STakashi Sakamoto 		s->data_block_counter = UINT_MAX;
824d67c46b9STakashi Sakamoto 	else
825d67c46b9STakashi Sakamoto 		s->data_block_counter = 0;
826d67c46b9STakashi Sakamoto 	s->data_block_state = initial_state[s->sfc].data_block;
827d67c46b9STakashi Sakamoto 	s->syt_offset_state = initial_state[s->sfc].syt_offset;
828d67c46b9STakashi Sakamoto 	s->last_syt_offset = TICKS_PER_CYCLE;
829d67c46b9STakashi Sakamoto 
830d67c46b9STakashi Sakamoto 	/* initialize packet buffer */
831d67c46b9STakashi Sakamoto 	if (s->direction == AMDTP_IN_STREAM) {
832d67c46b9STakashi Sakamoto 		dir = DMA_FROM_DEVICE;
833d67c46b9STakashi Sakamoto 		type = FW_ISO_CONTEXT_RECEIVE;
834d67c46b9STakashi Sakamoto 		header_size = IN_PACKET_HEADER_SIZE;
835d67c46b9STakashi Sakamoto 	} else {
836d67c46b9STakashi Sakamoto 		dir = DMA_TO_DEVICE;
837d67c46b9STakashi Sakamoto 		type = FW_ISO_CONTEXT_TRANSMIT;
838d67c46b9STakashi Sakamoto 		header_size = OUT_PACKET_HEADER_SIZE;
839d67c46b9STakashi Sakamoto 	}
840d67c46b9STakashi Sakamoto 	err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
841d67c46b9STakashi Sakamoto 				      amdtp_stream_get_max_payload(s), dir);
842d67c46b9STakashi Sakamoto 	if (err < 0)
843d67c46b9STakashi Sakamoto 		goto err_unlock;
844d67c46b9STakashi Sakamoto 
845d67c46b9STakashi Sakamoto 	s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
846d67c46b9STakashi Sakamoto 					   type, channel, speed, header_size,
847d67c46b9STakashi Sakamoto 					   amdtp_stream_first_callback, s);
848d67c46b9STakashi Sakamoto 	if (IS_ERR(s->context)) {
849d67c46b9STakashi Sakamoto 		err = PTR_ERR(s->context);
850d67c46b9STakashi Sakamoto 		if (err == -EBUSY)
851d67c46b9STakashi Sakamoto 			dev_err(&s->unit->device,
852d67c46b9STakashi Sakamoto 				"no free stream on this controller\n");
853d67c46b9STakashi Sakamoto 		goto err_buffer;
854d67c46b9STakashi Sakamoto 	}
855d67c46b9STakashi Sakamoto 
856d67c46b9STakashi Sakamoto 	amdtp_stream_update(s);
857d67c46b9STakashi Sakamoto 
8583b196c39STakashi Sakamoto 	if (s->flags & CIP_NO_HEADER)
8593b196c39STakashi Sakamoto 		s->tag = TAG_NO_CIP_HEADER;
8603b196c39STakashi Sakamoto 	else
8613b196c39STakashi Sakamoto 		s->tag = TAG_CIP;
8623b196c39STakashi Sakamoto 
863d67c46b9STakashi Sakamoto 	s->packet_index = 0;
864d67c46b9STakashi Sakamoto 	do {
865d67c46b9STakashi Sakamoto 		if (s->direction == AMDTP_IN_STREAM)
866d67c46b9STakashi Sakamoto 			err = queue_in_packet(s);
867d67c46b9STakashi Sakamoto 		else
868ff38e0c7STakashi Sakamoto 			err = queue_out_packet(s, 0);
869d67c46b9STakashi Sakamoto 		if (err < 0)
870d67c46b9STakashi Sakamoto 			goto err_context;
871d67c46b9STakashi Sakamoto 	} while (s->packet_index > 0);
872d67c46b9STakashi Sakamoto 
873d67c46b9STakashi Sakamoto 	/* NOTE: TAG1 matches CIP. This just affects in stream. */
874d67c46b9STakashi Sakamoto 	tag = FW_ISO_CONTEXT_MATCH_TAG1;
8753b196c39STakashi Sakamoto 	if ((s->flags & CIP_EMPTY_WITH_TAG0) || (s->flags & CIP_NO_HEADER))
876d67c46b9STakashi Sakamoto 		tag |= FW_ISO_CONTEXT_MATCH_TAG0;
877d67c46b9STakashi Sakamoto 
878d67c46b9STakashi Sakamoto 	s->callbacked = false;
879d67c46b9STakashi Sakamoto 	err = fw_iso_context_start(s->context, -1, 0, tag);
880d67c46b9STakashi Sakamoto 	if (err < 0)
881d67c46b9STakashi Sakamoto 		goto err_context;
882d67c46b9STakashi Sakamoto 
883d67c46b9STakashi Sakamoto 	mutex_unlock(&s->mutex);
884d67c46b9STakashi Sakamoto 
885d67c46b9STakashi Sakamoto 	return 0;
886d67c46b9STakashi Sakamoto 
887d67c46b9STakashi Sakamoto err_context:
888d67c46b9STakashi Sakamoto 	fw_iso_context_destroy(s->context);
889d67c46b9STakashi Sakamoto 	s->context = ERR_PTR(-1);
890d67c46b9STakashi Sakamoto err_buffer:
891d67c46b9STakashi Sakamoto 	iso_packets_buffer_destroy(&s->buffer, s->unit);
892d67c46b9STakashi Sakamoto err_unlock:
893d67c46b9STakashi Sakamoto 	mutex_unlock(&s->mutex);
894d67c46b9STakashi Sakamoto 
895d67c46b9STakashi Sakamoto 	return err;
896d67c46b9STakashi Sakamoto }
897d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_start);
898d67c46b9STakashi Sakamoto 
899d67c46b9STakashi Sakamoto /**
900d67c46b9STakashi Sakamoto  * amdtp_stream_pcm_pointer - get the PCM buffer position
901d67c46b9STakashi Sakamoto  * @s: the AMDTP stream that transports the PCM data
902d67c46b9STakashi Sakamoto  *
903d67c46b9STakashi Sakamoto  * Returns the current buffer position, in frames.
904d67c46b9STakashi Sakamoto  */
905d67c46b9STakashi Sakamoto unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
906d67c46b9STakashi Sakamoto {
9071dba9db0STakashi Sakamoto 	/*
9081dba9db0STakashi Sakamoto 	 * This function is called in software IRQ context of period_tasklet or
9091dba9db0STakashi Sakamoto 	 * process context.
9101dba9db0STakashi Sakamoto 	 *
9111dba9db0STakashi Sakamoto 	 * When the software IRQ context was scheduled by software IRQ context
9121dba9db0STakashi Sakamoto 	 * of IR/IT contexts, queued packets were already handled. Therefore,
9131dba9db0STakashi Sakamoto 	 * no need to flush the queue in buffer anymore.
9141dba9db0STakashi Sakamoto 	 *
9151dba9db0STakashi Sakamoto 	 * When the process context reach here, some packets will be already
9161dba9db0STakashi Sakamoto 	 * queued in the buffer. These packets should be handled immediately
9171dba9db0STakashi Sakamoto 	 * to keep better granularity of PCM pointer.
9181dba9db0STakashi Sakamoto 	 *
9191dba9db0STakashi Sakamoto 	 * Later, the process context will sometimes schedules software IRQ
9201dba9db0STakashi Sakamoto 	 * context of the period_tasklet. Then, no need to flush the queue by
9211dba9db0STakashi Sakamoto 	 * the same reason as described for IR/IT contexts.
9221dba9db0STakashi Sakamoto 	 */
9231dba9db0STakashi Sakamoto 	if (!in_interrupt() && amdtp_stream_running(s))
924d67c46b9STakashi Sakamoto 		fw_iso_context_flush_completions(s->context);
925d67c46b9STakashi Sakamoto 
926d67c46b9STakashi Sakamoto 	return ACCESS_ONCE(s->pcm_buffer_pointer);
927d67c46b9STakashi Sakamoto }
928d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
929d67c46b9STakashi Sakamoto 
930d67c46b9STakashi Sakamoto /**
931d67c46b9STakashi Sakamoto  * amdtp_stream_update - update the stream after a bus reset
932d67c46b9STakashi Sakamoto  * @s: the AMDTP stream
933d67c46b9STakashi Sakamoto  */
934d67c46b9STakashi Sakamoto void amdtp_stream_update(struct amdtp_stream *s)
935d67c46b9STakashi Sakamoto {
936d67c46b9STakashi Sakamoto 	/* Precomputing. */
937d67c46b9STakashi Sakamoto 	ACCESS_ONCE(s->source_node_id_field) =
938d67c46b9STakashi Sakamoto 		(fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) &
939d67c46b9STakashi Sakamoto 								CIP_SID_MASK;
940d67c46b9STakashi Sakamoto }
941d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_update);
942d67c46b9STakashi Sakamoto 
943d67c46b9STakashi Sakamoto /**
944d67c46b9STakashi Sakamoto  * amdtp_stream_stop - stop sending packets
945d67c46b9STakashi Sakamoto  * @s: the AMDTP stream to stop
946d67c46b9STakashi Sakamoto  *
947d67c46b9STakashi Sakamoto  * All PCM and MIDI devices of the stream must be stopped before the stream
948d67c46b9STakashi Sakamoto  * itself can be stopped.
949d67c46b9STakashi Sakamoto  */
950d67c46b9STakashi Sakamoto void amdtp_stream_stop(struct amdtp_stream *s)
951d67c46b9STakashi Sakamoto {
952d67c46b9STakashi Sakamoto 	mutex_lock(&s->mutex);
953d67c46b9STakashi Sakamoto 
954d67c46b9STakashi Sakamoto 	if (!amdtp_stream_running(s)) {
955d67c46b9STakashi Sakamoto 		mutex_unlock(&s->mutex);
956d67c46b9STakashi Sakamoto 		return;
957d67c46b9STakashi Sakamoto 	}
958d67c46b9STakashi Sakamoto 
959d67c46b9STakashi Sakamoto 	tasklet_kill(&s->period_tasklet);
960d67c46b9STakashi Sakamoto 	fw_iso_context_stop(s->context);
961d67c46b9STakashi Sakamoto 	fw_iso_context_destroy(s->context);
962d67c46b9STakashi Sakamoto 	s->context = ERR_PTR(-1);
963d67c46b9STakashi Sakamoto 	iso_packets_buffer_destroy(&s->buffer, s->unit);
964d67c46b9STakashi Sakamoto 
965d67c46b9STakashi Sakamoto 	s->callbacked = false;
966d67c46b9STakashi Sakamoto 
967d67c46b9STakashi Sakamoto 	mutex_unlock(&s->mutex);
968d67c46b9STakashi Sakamoto }
969d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_stop);
970d67c46b9STakashi Sakamoto 
971d67c46b9STakashi Sakamoto /**
972d67c46b9STakashi Sakamoto  * amdtp_stream_pcm_abort - abort the running PCM device
973d67c46b9STakashi Sakamoto  * @s: the AMDTP stream about to be stopped
974d67c46b9STakashi Sakamoto  *
975d67c46b9STakashi Sakamoto  * If the isochronous stream needs to be stopped asynchronously, call this
976d67c46b9STakashi Sakamoto  * function first to stop the PCM device.
977d67c46b9STakashi Sakamoto  */
978d67c46b9STakashi Sakamoto void amdtp_stream_pcm_abort(struct amdtp_stream *s)
979d67c46b9STakashi Sakamoto {
980d67c46b9STakashi Sakamoto 	struct snd_pcm_substream *pcm;
981d67c46b9STakashi Sakamoto 
982d67c46b9STakashi Sakamoto 	pcm = ACCESS_ONCE(s->pcm);
983d67c46b9STakashi Sakamoto 	if (pcm)
984d67c46b9STakashi Sakamoto 		snd_pcm_stop_xrun(pcm);
985d67c46b9STakashi Sakamoto }
986d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_abort);
987